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

Меч, зелья и награды

Отбиваемся мечом, лечимся зельем, прячемся зельем невидимости. Побеждённые гоблины роняют монеты и возвращаются, а охотник даёт квест на их уничтожение.

Структура

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

StarterPack
└─ Sword (Tool)
   ├─ Handle (Part) — клинок
   └─ SwordScript (Script)
ServerStorage
├─ Drop (Part) — мешочек с монетами
│  └─ DropScript (Script)
└─ Shop (Folder) — товары (урок «Магазин»)
   ├─ HealingPotion (Tool) — Handle → Drink (Sound); PotionScript
   └─ InvisiblePotion (Tool) — Handle; InvisibleScript
ServerScriptService
└─ EnemyManager (Script)
Workspace
└─ Hunter (Model) — QuestGiver: «Уничтожение», 3, 30

Шаги

  1. Меч: Tool «Sword», в нём Part «Handle» (0.3 × 4 × 0.8, Anchored и CanCollide выкл.). Tool → GripPos (0, −1.5, 0) — держим за низ. Script «SwordScript». Tool — в StarterPack.
  2. Взмах бьёт каждую цель один раз и вешает метку «creator» — кто ударил. По ней EnemyManager даёт монеты и прогресс квеста.
  3. Зелье лечения: Tool «HealingPotion», Handle (Anchored выкл.) со звуком «Drink», Script «PotionScript».
  4. Зелье невидимости: Tool «InvisiblePotion» со Script «InvisibleScript». Сначала зелье переносится в ServerScriptService — если удалить его сразу, удалится и скрипт, и игрок останется невидимым навсегда.
  5. Зелья положи в ServerStorage → Folder «Shop» — их будет продавать торговец. Для проверки скопируй в StarterPack.
  6. Награда: Part «Drop» (мешочек, Anchored выкл.) со Script «DropScript» — в ServerStorage.
  7. ServerScriptService → Script «EnemyManager»: возрождение врагов, Drop, монеты и квест. В методичке скрипт возрождения лежал в самом враге — и удалялся вместе с ним.
  8. Охотник «Hunter» — копия старосты с другими значениями: Title «Уничтожение», «Победи 3 гоблинов», 3, 30.

Скрипты

SwordScript

Где: StarterPack → Sword · Script
Зачем: Удар мечом.

-- игроки
local Players = game:GetService("Players")
-- удаляет объекты через время
local Debris = game:GetService("Debris")
-- меч
local tool = script.Parent
-- клинок
local handle = tool:WaitForChild("Handle")
-- урон за удар
local DAMAGE = 25
-- длина взмаха
local COOLDOWN = 0.6

-- анимация взмаха
local slash = Instance.new("Animation")
-- стандартная R15
slash.AnimationId = "rbxassetid://522635514"
-- идёт взмах
local swinging = false
-- кого уже ударили этим взмахом
local hitList = {}

-- клик мышью
tool.Activated:Connect(function()
    -- взмах ещё идёт
    if swinging then return end
    -- владелец
    local humanoid = tool.Parent:FindFirstChildOfClass("Humanoid")
    -- погибший не бьёт
    if not humanoid or humanoid.Health <= 0 then return end
    -- начали
    swinging = true
    -- новый взмах — список пуст
    table.clear(hitList)
    -- анимации владельца
    local animator = humanoid:FindFirstChildOfClass("Animator")
    -- взмах
    if animator then animator:LoadAnimation(slash):Play() end
    -- ждём
    task.wait(COOLDOWN)
    -- можно снова
    swinging = false
-- конец функции
end)

-- клинок коснулся
handle.Touched:Connect(function(hit)
    -- бьём только во время взмаха
    if not swinging then return end
    -- модель, которой принадлежит деталь
    local model = hit:FindFirstAncestorOfClass("Model")
    -- её Humanoid
    local target = model and model:FindFirstChildOfClass("Humanoid")
    -- не цель, сам себя или уже ударили
    if not target or target.Health <= 0 or model == tool.Parent or hitList[target] then return end
    -- игроков не бьём — только противников
    if Players:GetPlayerFromCharacter(model) then return end
    -- этим взмахом больше не бьём
    hitList[target] = true
    -- прошлая метка
    local old = target:FindFirstChild("creator")
    -- убираем
    if old then old:Destroy() end
    -- метка «кто ударил»
    local tag = Instance.new("ObjectValue")
    -- стандартное имя метки в Roblox
    tag.Name = "creator"
    -- игрок с мечом
    tag.Value = Players:GetPlayerFromCharacter(tool.Parent)
    -- в Humanoid противника
    tag.Parent = target
    -- через 2 секунды метка исчезнет
    Debris:AddItem(tag, 2)
    -- урон
    target:TakeDamage(DAMAGE)
-- конец функции
end)

PotionScript

Где: ServerStorage → Shop → HealingPotion · Script
Зачем: Лечит на 50.

-- удаляет объекты через время
local Debris = game:GetService("Debris")
-- зелье
local tool = script.Parent
-- сколько лечит
local HEAL = 50

-- клик — выпить
tool.Activated:Connect(function()
    -- кто пьёт
    local humanoid = tool.Parent:FindFirstChildOfClass("Humanoid")
    -- погибшим не помочь
    if not humanoid or humanoid.Health <= 0 then return end
    -- +50, но не больше максимума
    humanoid.Health = math.min(humanoid.Health + HEAL, humanoid.MaxHealth)
    -- звук глотка
    local sound = tool.Handle:FindFirstChild("Drink")
    -- звук есть
    if sound and humanoid.RootPart then
        -- переносим в игрока — иначе пропадёт вместе с зельем
        sound.Parent = humanoid.RootPart
        -- буль
        sound:Play()
        -- потом удалим
        Debris:AddItem(sound, 3)
    -- конец проверки
    end
    -- зелье выпито
    tool:Destroy()
-- конец функции
end)

InvisibleScript

Где: ServerStorage → Shop → InvisiblePotion · Script
Зачем: Невидимость на 5 секунд.

-- здесь скрипт доработает после того, как зелье пропадёт из рук
local SSS = game:GetService("ServerScriptService")
-- зелье
local tool = script.Parent
-- секунд невидимости
local DURATION = 5
-- уже выпито
local used = false

-- плавно меняет прозрачность
local function Fade(saved, toInvisible)
    -- 10 шагов
    for i = 1, 10 do
        -- доля невидимости
        local k = toInvisible and i / 10 or 1 - i / 10
        -- каждая деталь
        for obj, base in pairs(saved) do
            -- от своей прозрачности к 1
            if obj.Parent then obj.Transparency = base + (1 - base) * k end
        -- конец перебора
        end
        -- полсекунды на всё
        task.wait(0.05)
    -- конец цикла
    end
-- конец функции
end

-- клик — выпить
tool.Activated:Connect(function()
    -- уже
    if used then return end
    -- кто пьёт
    local character = tool.Parent
    -- Humanoid
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    -- погибший не пьёт
    if not humanoid or humanoid.Health <= 0 then return end
    -- выпито
    used = true
    -- убираем из рук, но скрипт продолжает работать
    tool.Parent = SSS
    -- деталь → её прозрачность
    local saved = {}
    -- всё в персонаже
    for _, obj in ipairs(character:GetDescendants()) do
        -- видимые детали и лицо
        if (obj:IsA("BasePart") or obj:IsA("Decal")) and obj.Name ~= "HumanoidRootPart" then
            -- запоминаем
            saved[obj] = obj.Transparency
        -- конец проверки
        end
    -- конец перебора
    end
    -- противники перестают видеть
    character:SetAttribute("Invisible", true)
    -- исчезаем
    Fade(saved, true)
    -- ждём
    task.wait(DURATION)
    -- появляемся
    Fade(saved, false)
    -- снова виден
    character:SetAttribute("Invisible", nil)
    -- теперь зелье можно удалить
    tool:Destroy()
-- конец функции
end)

DropScript

Где: ServerStorage → Drop · Script
Зачем: Мешочек с монетами.

-- игроки
local Players = game:GetService("Players")
-- мешочек
local drop = script.Parent
-- сколько монет внутри
local COINS = 5
-- через сколько секунд исчезнет
local LIFETIME = 30
-- уже подобран
local taken = false

-- касание
drop.Touched:Connect(function(hit)
    -- уже взяли
    if taken then return end
    -- игрок?
    local player = Players:GetPlayerFromCharacter(hit.Parent)
    -- его таблица
    local stats = player and player:FindFirstChild("leaderstats")
    -- не игрок
    if not stats then return end
    -- больше не берётся
    taken = true
    -- монеты
    stats.Coins.Value += COINS
    -- мешочек пропал
    drop:Destroy()
-- конец функции
end)

-- ждём
task.wait(LIFETIME)
-- никто не взял — убираем
if drop.Parent then drop:Destroy() end

EnemyManager

Где: ServerScriptService · Script
Зачем: Возрождение, награды, квест.

-- здесь лежит награда Drop
local SS = game:GetService("ServerStorage")
-- все противники
local enemies = workspace:WaitForChild("Enemies")
-- через сколько секунд противник вернётся
local RESPAWN_TIME = 10
-- монет за победу
local KILL_REWARD = 3
-- квест, которому засчитываются победы
local KILL_QUEST = "Уничтожение"

-- следим за одним противником
local function Setup(enemy)
    -- копия целого — для возрождения
    local sample = enemy:Clone()
    -- где стоял
    local spot = enemy:GetPivot()
    -- Humanoid
    local humanoid = enemy:WaitForChild("Humanoid")
    -- противник погиб
    humanoid.Died:Connect(function()
        -- метка от меча
        local tag = humanoid:FindFirstChild("creator")
        -- кто победил
        local player = tag and tag.Value
        -- игрок ещё в игре
        if player and player.Parent then
            -- таблица
            local stats = player:FindFirstChild("leaderstats")
            -- монеты
            if stats then stats.Coins.Value += KILL_REWARD end
            -- квест
            local quest = player:FindFirstChild("QuestInfo")
            -- взят квест на противников
            if quest and quest.Title.Value == KILL_QUEST then
                -- +1
                quest.Progress.Value += 1
            -- конец проверки
            end
        -- конец проверки
        end
        -- мешочек с монетами
        local drop = SS:FindFirstChild("Drop")
        -- есть
        if drop then
            -- копия
            local copy = drop:Clone()
            -- там, где упал противник
            copy:PivotTo(CFrame.new(enemy:GetPivot().Position))
            -- в мир
            copy.Parent = workspace
        -- конец проверки
        end
        -- ждём
        task.wait(RESPAWN_TIME)
        -- убираем тело
        enemy:Destroy()
        -- новый противник
        local new = sample:Clone()
        -- на старое место
        new:PivotTo(spot)
        -- в игру — его EnemyScript запустится сам
        new.Parent = enemies
        -- следим и за ним
        Setup(new)
    -- конец функции
    end)
-- конец функции
end

-- все противники
for _, enemy in ipairs(enemies:GetChildren()) do
    -- следим
    Setup(enemy)
-- конец перебора
end

Проверка

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

Roblox · Adventure · lesson 4

Sword, potions and rewards

We fight with a sword, heal with a potion and hide with an invisibility potion. Defeated goblins drop coins and come back, and the hunter gives a quest to defeat them.

Structure

Blue — what we add in this lesson.

StarterPack
└─ Sword (Tool)
   ├─ Handle (Part) — the blade
   └─ SwordScript (Script)
ServerStorage
├─ Drop (Part) — a coin pouch
│  └─ DropScript (Script)
└─ Shop (Folder) — goods (the Shop lesson)
   ├─ HealingPotion (Tool) — Handle → Drink (Sound); PotionScript
   └─ InvisiblePotion (Tool) — Handle; InvisibleScript
ServerScriptService
└─ EnemyManager (Script)
Workspace
└─ Hunter (Model) — QuestGiver: "Уничтожение" (Extermination), 3, 30

Steps

  1. Sword: a Tool "Sword" with a Part "Handle" (0.3 × 4 × 0.8, Anchored and CanCollide off). Tool → GripPos (0, −1.5, 0) — held by the bottom. A Script "SwordScript". The Tool goes in StarterPack.
  2. A swing hits each target once and tags it with "creator" — who hit it. EnemyManager uses the tag to give coins and quest progress.
  3. Healing potion: a Tool "HealingPotion", a Handle (Anchored off) with a sound "Drink", a Script "PotionScript".
  4. Invisibility potion: a Tool "InvisiblePotion" with a Script "InvisibleScript". The potion is first moved to ServerScriptService — if you delete it right away, the script is deleted too and the player stays invisible forever.
  5. Put the potions in ServerStorage → Folder "Shop" — the trader will sell them. Copy them into StarterPack for testing.
  6. Reward: a Part "Drop" (a pouch, Anchored off) with a Script "DropScript" — in ServerStorage.
  7. ServerScriptService → Script "EnemyManager": enemy respawning, Drop, coins and the quest. In the workbook the respawn script lived inside the enemy — and was deleted along with it.
  8. The hunter "Hunter" is a copy of the elder with other values: Title "Уничтожение", "Победи 3 гоблинов" (defeat 3 goblins), 3, 30.

Scripts

SwordScript

Where: StarterPack → Sword · Script
Why: A sword hit.

-- players
local Players = game:GetService("Players")
-- deletes objects after a delay
local Debris = game:GetService("Debris")
-- the sword
local tool = script.Parent
-- the blade
local handle = tool:WaitForChild("Handle")
-- damage per hit
local DAMAGE = 25
-- swing duration
local COOLDOWN = 0.6

-- the swing animation
local slash = Instance.new("Animation")
-- the standard R15 one
slash.AnimationId = "rbxassetid://522635514"
-- a swing is in progress
local swinging = false
-- who has already been hit by this swing
local hitList = {}

-- a mouse click
tool.Activated:Connect(function()
    -- the swing is still going
    if swinging then return end
    -- the owner
    local humanoid = tool.Parent:FindFirstChildOfClass("Humanoid")
    -- the dead don't attack
    if not humanoid or humanoid.Health <= 0 then return end
    -- started
    swinging = true
    -- a new swing — the list is empty
    table.clear(hitList)
    -- the owner's animations
    local animator = humanoid:FindFirstChildOfClass("Animator")
    -- the swing
    if animator then animator:LoadAnimation(slash):Play() end
    -- wait
    task.wait(COOLDOWN)
    -- can fire again
    swinging = false
-- end of function
end)

-- the blade touched
handle.Touched:Connect(function(hit)
    -- hit only during a swing
    if not swinging then return end
    -- the model the part belongs to
    local model = hit:FindFirstAncestorOfClass("Model")
    -- its Humanoid
    local target = model and model:FindFirstChildOfClass("Humanoid")
    -- not a target, ourselves, or already hit
    if not target or target.Health <= 0 or model == tool.Parent or hitList[target] then return end
    -- don't hit players — only enemies
    if Players:GetPlayerFromCharacter(model) then return end
    -- don't hit again with this swing
    hitList[target] = true
    -- the previous tag
    local old = target:FindFirstChild("creator")
    -- remove it
    if old then old:Destroy() end
    -- the "who hit" tag
    local tag = Instance.new("ObjectValue")
    -- the standard Roblox tag name
    tag.Name = "creator"
    -- the player with the sword
    tag.Value = Players:GetPlayerFromCharacter(tool.Parent)
    -- into the enemy's Humanoid
    tag.Parent = target
    -- the tag disappears after 2 seconds
    Debris:AddItem(tag, 2)
    -- damage
    target:TakeDamage(DAMAGE)
-- end of function
end)

PotionScript

Where: ServerStorage → Shop → HealingPotion · Script
Why: Heals 50.

-- deletes objects after a delay
local Debris = game:GetService("Debris")
-- the potion
local tool = script.Parent
-- how much it heals
local HEAL = 50

-- click — drink
tool.Activated:Connect(function()
    -- who drinks
    local humanoid = tool.Parent:FindFirstChildOfClass("Humanoid")
    -- the dead can't be helped
    if not humanoid or humanoid.Health <= 0 then return end
    -- +50, but not above the maximum
    humanoid.Health = math.min(humanoid.Health + HEAL, humanoid.MaxHealth)
    -- the gulp sound
    local sound = tool.Handle:FindFirstChild("Drink")
    -- there is a sound
    if sound and humanoid.RootPart then
        -- move it into the player — otherwise it disappears with the potion
        sound.Parent = humanoid.RootPart
        -- glug
        sound:Play()
        -- delete it later
        Debris:AddItem(sound, 3)
    -- end of check
    end
    -- the potion is drunk
    tool:Destroy()
-- end of function
end)

InvisibleScript

Where: ServerStorage → Shop → InvisiblePotion · Script
Why: Invisibility for 5 seconds.

-- here the script keeps running after the potion leaves the hands
local SSS = game:GetService("ServerScriptService")
-- the potion
local tool = script.Parent
-- seconds of invisibility
local DURATION = 5
-- already drunk
local used = false

-- changes transparency smoothly
local function Fade(saved, toInvisible)
    -- 10 steps
    for i = 1, 10 do
        -- the share of invisibility
        local k = toInvisible and i / 10 or 1 - i / 10
        -- each part
        for obj, base in pairs(saved) do
            -- from its own transparency to 1
            if obj.Parent then obj.Transparency = base + (1 - base) * k end
        -- end of loop
        end
        -- half a second in total
        task.wait(0.05)
    -- end of loop
    end
-- end of function
end

-- click — drink
tool.Activated:Connect(function()
    -- already
    if used then return end
    -- who drinks
    local character = tool.Parent
    -- Humanoid
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    -- the dead don't drink
    if not humanoid or humanoid.Health <= 0 then return end
    -- drunk
    used = true
    -- remove it from the hands, but the script keeps working
    tool.Parent = SSS
    -- part → its transparency
    local saved = {}
    -- everything in the character
    for _, obj in ipairs(character:GetDescendants()) do
        -- visible parts and the face
        if (obj:IsA("BasePart") or obj:IsA("Decal")) and obj.Name ~= "HumanoidRootPart" then
            -- remember
            saved[obj] = obj.Transparency
        -- end of check
        end
    -- end of loop
    end
    -- enemies stop seeing
    character:SetAttribute("Invisible", true)
    -- fade out
    Fade(saved, true)
    -- wait
    task.wait(DURATION)
    -- fade in
    Fade(saved, false)
    -- visible again
    character:SetAttribute("Invisible", nil)
    -- now the potion can be deleted
    tool:Destroy()
-- end of function
end)

DropScript

Where: ServerStorage → Drop · Script
Why: A coin pouch.

-- players
local Players = game:GetService("Players")
-- the pouch
local drop = script.Parent
-- how many coins are inside
local COINS = 5
-- how many seconds until it disappears
local LIFETIME = 30
-- already picked up
local taken = false

-- touch
drop.Touched:Connect(function(hit)
    -- already taken
    if taken then return end
    -- a player?
    local player = Players:GetPlayerFromCharacter(hit.Parent)
    -- their board
    local stats = player and player:FindFirstChild("leaderstats")
    -- not a player
    if not stats then return end
    -- can't be taken again
    taken = true
    -- coins
    stats.Coins.Value += COINS
    -- the pouch is gone
    drop:Destroy()
-- end of function
end)

-- wait
task.wait(LIFETIME)
-- nobody took it — remove it
if drop.Parent then drop:Destroy() end

EnemyManager

Where: ServerScriptService · Script
Why: Respawning, rewards, the quest.

-- the Drop reward is kept here
local SS = game:GetService("ServerStorage")
-- all enemies
local enemies = workspace:WaitForChild("Enemies")
-- how many seconds until an enemy returns
local RESPAWN_TIME = 10
-- coins per win
local KILL_REWARD = 3
-- the quest that counts the wins
local KILL_QUEST = "Уничтожение"

-- watch one enemy
local function Setup(enemy)
    -- a copy of the whole thing — for respawning
    local sample = enemy:Clone()
    -- where it stood
    local spot = enemy:GetPivot()
    -- Humanoid
    local humanoid = enemy:WaitForChild("Humanoid")
    -- the enemy died
    humanoid.Died:Connect(function()
        -- the tag from the sword
        local tag = humanoid:FindFirstChild("creator")
        -- who won
        local player = tag and tag.Value
        -- the player is still in the game
        if player and player.Parent then
            -- the board
            local stats = player:FindFirstChild("leaderstats")
            -- coins
            if stats then stats.Coins.Value += KILL_REWARD end
            -- the quest
            local quest = player:FindFirstChild("QuestInfo")
            -- the enemy quest is taken
            if quest and quest.Title.Value == KILL_QUEST then
                -- +1
                quest.Progress.Value += 1
            -- end of check
            end
        -- end of check
        end
        -- a coin pouch
        local drop = SS:FindFirstChild("Drop")
        -- yes
        if drop then
            -- a copy
            local copy = drop:Clone()
            -- where the enemy fell
            copy:PivotTo(CFrame.new(enemy:GetPivot().Position))
            -- into the world
            copy.Parent = workspace
        -- end of check
        end
        -- wait
        task.wait(RESPAWN_TIME)
        -- remove the body
        enemy:Destroy()
        -- a new enemy
        local new = sample:Clone()
        -- in the old place
        new:PivotTo(spot)
        -- into the game — its EnemyScript starts by itself
        new.Parent = enemies
        -- watch it too
        Setup(new)
    -- end of function
    end)
-- end of function
end

-- all enemies
for _, enemy in ipairs(enemies:GetChildren()) do
    -- watch
    Setup(enemy)
-- end of loop
end

Testing

Press Play (or Run) and make sure: