Roblox · Приключение · урок 6

День и ночь, подсказки и публикация

Мир живёт: солнце садится, загораются фонари, а в важных местах появляются подсказки.

Структура

Синим — что добавляем на этом уроке.

Workspace
├─ Lights (Folder) — фонари с PointLight
└─ Hints (Folder)
   └─ Hint (Part) — невидимая зона
      └─ Text (StringValue) — текст подсказки
ServerScriptService
└─ DayNight (Script)
StarterGui
└─ HintGui (ScreenGui) — ResetOnSpawn выкл.
   ├─ HintLabel (TextLabel) — внизу, Visible выкл.
   └─ HintLocal (LocalScript)

Шаги

  1. Фонари: столб + плафон с PointLight. Все фонари — в папку «Lights».
  2. ServerScriptService → Script «DayNight»: ClockTime растёт на 0.05 в секунду — сутки за 8 минут. С 18 до 6 фонари горят.
  3. Подсказки: невидимые детали (Transparency 1, CanCollide выкл.) в папке «Hints», в каждой StringValue «Text».
  4. HintGui → TextLabel «HintLabel» внизу экрана и LocalScript «HintLocal». Подсказка проверяется у каждого игрока своя и показывается один раз (в методичке — всем сразу и много раз).
  5. Workspace → StreamingEnabled выкл., чтобы локальный скрипт сразу видел все зоны подсказок.
  6. Пройди игру целиком: диалоги, оба квеста, гоблины, магазин, ночь.
  7. File → Publish to Roblox, жанр Adventure. Game Settings → Permissions → Public.

Скрипты

DayNight

Где: 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

HintLocal

Где: 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

Day and night, hints and publishing

The world comes alive: the sun sets, lamps light up, and hints appear in important places.

Structure

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)

Steps

  1. Lamps: a post + a shade with a PointLight. All lamps go into a folder "Lights".
  2. ServerScriptService → Script "DayNight": ClockTime grows by 0.05 per second — a day takes 8 minutes. From 18 to 6 the lamps are on.
  3. Hints: invisible parts (Transparency 1, CanCollide off) in a folder "Hints", each with a StringValue "Text".
  4. HintGui → TextLabel "HintLabel" at the bottom of the screen and a LocalScript "HintLocal". Each player's hints are checked separately and shown once (in the workbook — to everyone at once, many times).
  5. Workspace → StreamingEnabled off so the local script sees all hint zones right away.
  6. Play through the whole game: dialogs, both quests, goblins, the shop, night.
  7. File → Publish to Roblox, genre Adventure. Game Settings → Permissions → Public.

Scripts

DayNight

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
end

HintLocal

Where: 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)

Testing

Press Play (or Run) and make sure: