Roblox · Приключение · урок 6
Мир живёт: солнце садится, загораются фонари, а в важных местах появляются подсказки.
Синим — что добавляем на этом уроке.
Workspace ├─ Lights (Folder) — фонари с PointLight └─ Hints (Folder) └─ Hint (Part) — невидимая зона └─ Text (StringValue) — текст подсказки ServerScriptService └─ DayNight (Script) StarterGui └─ HintGui (ScreenGui) — ResetOnSpawn выкл. ├─ HintLabel (TextLabel) — внизу, Visible выкл. └─ HintLocal (LocalScript)
Где: ServerScriptService · Script
Зачем: Смена дня и ночи, фонари.
-- освещение
local Lighting = game:GetService("Lighting")
-- фонари
local lights = workspace:WaitForChild("Lights")
-- часов игрового времени за секунду: сутки = 8 минут
local STEP = 0.05
-- бесконечно
while true do
-- время идёт; после 24 — снова 0
Lighting.ClockTime = (Lighting.ClockTime + STEP) % 24
-- с 18 до 6 — ночь
local night = Lighting.ClockTime >= 18 or Lighting.ClockTime < 6
-- всё в фонарях
for _, light in ipairs(lights:GetDescendants()) do
-- ночью свет горит
if light:IsA("Light") then light.Enabled = night end
-- конец перебора
end
-- секунда
task.wait(1)
-- конец цикла
endГде: StarterGui → HintGui · LocalScript
Зачем: Подсказки.
-- игроки
local Players = game:GetService("Players")
-- этот игрок
local player = Players.LocalPlayer
-- зоны подсказок
local hints = workspace:WaitForChild("Hints")
-- надпись
local label = script.Parent:WaitForChild("HintLabel")
-- секунд на экране
local SHOW_TIME = 4
-- какие подсказки уже были
local shown = {}
-- следим за одной зоной
local function Watch(trigger)
-- касание
trigger.Touched:Connect(function(hit)
-- уже было или это не мы
if shown[trigger] or hit.Parent ~= player.Character then return end
-- StringValue с текстом
local text = trigger:FindFirstChild("Text")
-- текста нет
if not text then return end
-- больше не показываем
shown[trigger] = true
-- текст
label.Text = text.Value
-- показываем
label.Visible = true
-- через 4 секунды
task.delay(SHOW_TIME, function()
-- прячем, если не появилась другая
if label.Text == text.Value then label.Visible = false end
-- конец функции
end)
-- конец функции
end)
-- конец функции
end
-- все зоны
for _, trigger in ipairs(hints:GetChildren()) do Watch(trigger) end
-- и новые
hints.ChildAdded:Connect(Watch)Запусти Play (или Run) и убедись:
Roblox · Adventure · lesson 6
The world comes alive: the sun sets, lamps light up, and hints appear in important places.
Blue — what we add in this lesson.
Workspace ├─ Lights (Folder) — lamps with a PointLight └─ Hints (Folder) └─ Hint (Part) — an invisible zone └─ Text (StringValue) — the hint text ServerScriptService └─ DayNight (Script) StarterGui └─ HintGui (ScreenGui) — ResetOnSpawn off ├─ HintLabel (TextLabel) — at the bottom, Visible off └─ HintLocal (LocalScript)
Where: ServerScriptService · Script
Why: Day and night cycle, lamps.
-- lighting
local Lighting = game:GetService("Lighting")
-- lamps
local lights = workspace:WaitForChild("Lights")
-- game hours per second: a day = 8 minutes
local STEP = 0.05
-- forever
while true do
-- time passes; after 24 — back to 0
Lighting.ClockTime = (Lighting.ClockTime + STEP) % 24
-- night from 18 to 6
local night = Lighting.ClockTime >= 18 or Lighting.ClockTime < 6
-- everything in the lamps
for _, light in ipairs(lights:GetDescendants()) do
-- the light is on at night
if light:IsA("Light") then light.Enabled = night end
-- end of loop
end
-- a second
task.wait(1)
-- end of loop
endWhere: StarterGui → HintGui · LocalScript
Why: Hints.
-- players
local Players = game:GetService("Players")
-- this player
local player = Players.LocalPlayer
-- hint zones
local hints = workspace:WaitForChild("Hints")
-- a label
local label = script.Parent:WaitForChild("HintLabel")
-- seconds on screen
local SHOW_TIME = 4
-- which hints have already been shown
local shown = {}
-- watch one zone
local function Watch(trigger)
-- touch
trigger.Touched:Connect(function(hit)
-- already happened or it's not us
if shown[trigger] or hit.Parent ~= player.Character then return end
-- a StringValue with the text
local text = trigger:FindFirstChild("Text")
-- no text
if not text then return end
-- don't show it again
shown[trigger] = true
-- text
label.Text = text.Value
-- show it
label.Visible = true
-- after 4 seconds
task.delay(SHOW_TIME, function()
-- hide it unless another one appeared
if label.Text == text.Value then label.Visible = false end
-- end of function
end)
-- end of function
end)
-- end of function
end
-- all zones
for _, trigger in ipairs(hints:GetChildren()) do Watch(trigger) end
-- and new ones
hints.ChildAdded:Connect(Watch)Press Play (or Run) and make sure: