Roblox · Обби · урок 5

Чекпоинты

Длинную полосу никто не пройдёт с начала после каждой ошибки. Чекпоинты запоминают этап, а таблица по Tab показывает, кто дальше всех.

Структура

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

Workspace
└─ Checkpoints (Folder)
   ├─ 0 (Part) — старт
   └─ 1, 2, 3… (Part) — чекпоинты по порядку
ServerScriptService
└─ Checkpoints (Script)

Шаги

  1. Workspace → Folder «Checkpoints». В нём плоские детали, названные числами: «0» — на старте, рядом со SpawnLocation, дальше «1», «2», «3»… по ходу полосы.
  2. Чекпоинты Anchored вкл., CanCollide вкл. — на них стоят. Яркий цвет или Neon, чтобы их было видно.
  3. ServerScriptService → Script «Checkpoints».
  4. leaderstats — папка с таким именем в игроке: Roblox сам показывает её значения в таблице по Tab. Stage — этап.
  5. CharacterAdded срабатывает после каждой смерти: скрипт ставит персонажа на чекпоинт его этапа.
  6. Засчитывается только следующий номер: со 2 чекпоинта нельзя «перепрыгнуть» на 5, и назад этап не уменьшается.

Скрипты

Checkpoints

Где: ServerScriptService · Script
Зачем: Этапы игроков и появление на чекпоинте.

-- сервис игроков
local Players = game:GetService("Players")
-- папка с чекпоинтами 0, 1, 2…
local checkpoints = workspace:WaitForChild("Checkpoints")

-- игрок зашёл
Players.PlayerAdded:Connect(function(player)
    -- таблица по Tab
    local leaderstats = Instance.new("Folder")
    -- имя строго такое
    leaderstats.Name = "leaderstats"
    -- в игрока
    leaderstats.Parent = player
    -- номер этапа
    local stage = Instance.new("IntValue")
    -- столбец «Stage»
    stage.Name = "Stage"
    -- в таблицу
    stage.Parent = leaderstats

    -- персонаж появился (и после каждой смерти)
    player.CharacterAdded:Connect(function(character)
        -- Humanoid персонажа
        local humanoid = character:WaitForChild("Humanoid")
        -- ждём, пока персонаж соберётся
        character:WaitForChild("HumanoidRootPart")
        -- чекпоинт текущего этапа
        local point = checkpoints:FindFirstChild(tostring(stage.Value))
        -- нашёлся
        if point then
            -- даём Roblox закончить появление на SpawnLocation
            task.wait(0.1)
            -- ставим над чекпоинтом
            character:PivotTo(point.CFrame + Vector3.new(0, 3, 0))
        -- конец проверки
        end
        -- персонаж чего-то коснулся
        humanoid.Touched:Connect(function(hit)
            -- это следующий по номеру чекпоинт
            if hit.Parent == checkpoints and tonumber(hit.Name) == stage.Value + 1 then
                -- этап пройден
                stage.Value += 1
            -- конец проверки
            end
        -- конец функции
        end)
    -- конец функции
    end)
-- конец функции
end)

Проверка

Запусти Play (или Run) и убедись:

Roblox · Obby · lesson 5

Checkpoints

Nobody will finish a long course if they restart after every mistake. Checkpoints remember the stage, and the Tab board shows who got furthest.

Structure

Blue — what we add in this lesson.

Workspace
└─ Checkpoints (Folder)
   ├─ 0 (Part) — start
   └─ 1, 2, 3… (Part) — checkpoints in order
ServerScriptService
└─ Checkpoints (Script)

Steps

  1. Workspace → Folder "Checkpoints". Inside are flat parts named with numbers: "0" at the start next to the SpawnLocation, then "1", "2", "3"… along the course.
  2. Checkpoints: Anchored on, CanCollide on — players stand on them. A bright color or Neon so they're visible.
  3. ServerScriptService → Script «Checkpoints».
  4. leaderstats is a folder with exactly this name inside the player: Roblox shows its values on the Tab board by itself. Stage is the stage.
  5. CharacterAdded fires after every death: the script places the character on its stage's checkpoint.
  6. Only the next number counts: you can't "skip" from checkpoint 2 to 5, and the stage never goes down.

Scripts

Checkpoints

Where: ServerScriptService · Script
Why: Player stages and respawning on the checkpoint.

-- the Players service
local Players = game:GetService("Players")
-- the folder with checkpoints 0, 1, 2…
local checkpoints = workspace:WaitForChild("Checkpoints")

-- a player joined
Players.PlayerAdded:Connect(function(player)
    -- the Tab board
    local leaderstats = Instance.new("Folder")
    -- the name must be exactly this
    leaderstats.Name = "leaderstats"
    -- into the player
    leaderstats.Parent = player
    -- stage number
    local stage = Instance.new("IntValue")
    -- the "Stage" column
    stage.Name = "Stage"
    -- into the board
    stage.Parent = leaderstats

    -- the character spawned (also after every death)
    player.CharacterAdded:Connect(function(character)
        -- the character's Humanoid
        local humanoid = character:WaitForChild("Humanoid")
        -- wait until the character is assembled
        character:WaitForChild("HumanoidRootPart")
        -- the checkpoint of the current stage
        local point = checkpoints:FindFirstChild(tostring(stage.Value))
        -- found
        if point then
            -- let Roblox finish spawning on the SpawnLocation
            task.wait(0.1)
            -- place above the checkpoint
            character:PivotTo(point.CFrame + Vector3.new(0, 3, 0))
        -- end of check
        end
        -- the character touched something
        humanoid.Touched:Connect(function(hit)
            -- it's the next checkpoint by number
            if hit.Parent == checkpoints and tonumber(hit.Name) == stage.Value + 1 then
                -- stage passed
                stage.Value += 1
            -- end of check
            end
        -- end of function
        end)
    -- end of function
    end)
-- end of function
end)

Testing

Press Play (or Run) and make sure: