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

Скрипты: убивающий и ускоряющий блоки

Первые скрипты: блок, который убивает при касании, и блок, который разгоняет игрока. Это основа почти всех ловушек в обби.

Структура

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

Workspace
├─ KillBrick (Part) — красный, Material Neon
│  └─ KillScript (Script)
├─ SpeedBrick (Part) — зелёный
│  └─ SpeedScript (Script)
└─ NormalBrick (Part) — возвращает скорость
   └─ NormalSpeedScript (Script)

Шаги

  1. Деталь «KillBrick»: красная, Material Neon — игроки сразу понимают, что это опасно. Anchored вкл.
  2. Наведи на KillBrick → «+» → Script, назови «KillScript». Script внутри детали работает с ней через script.Parent.
  3. Touched — событие касания: срабатывает, когда деталь задевает что угодно. У персонажа есть Humanoid — по нему отличаем игрока от других деталей.
  4. Деталь «SpeedBrick» (зелёная) со скриптом «SpeedScript»: WalkSpeed — скорость ходьбы, обычная 16.
  5. Домашнее задание из методички: «NormalBrick» возвращает скорость 16. Это тот же скрипт с другим числом.
  6. Расставь убивающие блоки на полосе: полоски поперёк платформы, через которые надо перепрыгнуть.

Скрипты

KillScript

Где: Workspace → KillBrick · Script
Зачем: Убивает игрока при касании.

-- деталь, в которой лежит скрипт
local part = script.Parent

-- когда что-то касается детали
part.Touched:Connect(function(hit)
    -- Humanoid есть только у персонажей
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    -- коснулся персонаж
    if humanoid then
        -- здоровье в ноль — игрок погибает
        humanoid.Health = 0
    -- конец проверки
    end
-- конец функции
end)

SpeedScript

Где: Workspace → SpeedBrick · Script
Зачем: Разгоняет игрока до 25.

-- деталь со скриптом
local part = script.Parent
-- новая скорость; обычная — 16
local SPEED = 25

-- касание
part.Touched:Connect(function(hit)
    -- ищем Humanoid
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    -- это персонаж
    if humanoid then
        -- меняем скорость ходьбы
        humanoid.WalkSpeed = SPEED
    -- конец проверки
    end
-- конец функции
end)

NormalSpeedScript

Где: Workspace → NormalBrick · Script
Зачем: Возвращает обычную скорость 16.

-- деталь со скриптом
local part = script.Parent
-- обычная скорость Roblox
local SPEED = 16

-- касание
part.Touched:Connect(function(hit)
    -- ищем Humanoid
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    -- это персонаж
    if humanoid then
        -- меняем скорость ходьбы
        humanoid.WalkSpeed = SPEED
    -- конец проверки
    end
-- конец функции
end)

Проверка

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

Roblox · Obby · lesson 2

Scripts: kill and speed blocks

First scripts: a block that kills on touch and a block that speeds the player up. These are the basis of almost every obby trap.

Structure

Blue — what we add in this lesson.

Workspace
├─ KillBrick (Part) — red, Material Neon
│  └─ KillScript (Script)
├─ SpeedBrick (Part) — green
│  └─ SpeedScript (Script)
└─ NormalBrick (Part) — restores speed
   └─ NormalSpeedScript (Script)

Steps

  1. A part "KillBrick": red, Material Neon — players see right away that it's dangerous. Anchored on.
  2. Hover over KillBrick → "+" → Script, name it "KillScript". A Script inside a part works with it through script.Parent.
  3. Touched is the touch event: it fires when anything touches the part. A character has a Humanoid — that's how we tell a player from other parts.
  4. A part "SpeedBrick" (green) with a script "SpeedScript": WalkSpeed is the walking speed, normally 16.
  5. Homework from the workbook: "NormalBrick" restores speed 16. It's the same script with a different number.
  6. Place kill blocks along the course: strips across a platform that you have to jump over.

Scripts

KillScript

Where: Workspace → KillBrick · Script
Why: Kills the player on touch.

-- the part that holds the script
local part = script.Parent

-- when something touches the part
part.Touched:Connect(function(hit)
    -- only characters have a Humanoid
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    -- a character touched it
    if humanoid then
        -- health to zero — the player dies
        humanoid.Health = 0
    -- end of check
    end
-- end of function
end)

SpeedScript

Where: Workspace → SpeedBrick · Script
Why: Speeds the player up to 25.

-- the part with the script
local part = script.Parent
-- the new speed; normal is 16
local SPEED = 25

-- touch
part.Touched:Connect(function(hit)
    -- look for a Humanoid
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    -- it's a character
    if humanoid then
        -- change the walking speed
        humanoid.WalkSpeed = SPEED
    -- end of check
    end
-- end of function
end)

NormalSpeedScript

Where: Workspace → NormalBrick · Script
Why: Restores the normal speed of 16.

-- the part with the script
local part = script.Parent
-- the normal Roblox speed
local SPEED = 16

-- touch
part.Touched:Connect(function(hit)
    -- look for a Humanoid
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    -- it's a character
    if humanoid then
        -- change the walking speed
        humanoid.WalkSpeed = SPEED
    -- end of check
    end
-- end of function
end)

Testing

Press Play (or Run) and make sure: