ダメージを受けるパーツ

ダメージ床。床の上を歩くとダメージを受けるようにします。触るとダメージを受けるので、ダメージ壁としても使用できます。

  1. Workspace に Part を1つ追加します。
  2. Part に Script を追加します。

床(Part) の上を歩くと、ライフが1ずつ減っていきます。止まった場合はダメージを受けません。

local part = script.Parent

local function onTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
 
    if humanoid then
        humanoid.Health = humanoid.Health - 1    -- 数字を大きくすると沢山ダメージを受けます
    end
end
 
part.Touched:Connect(onTouch)    -- Touched イベントの設定

Humanoid の TakeDamage というメソッドを使用することもできます。

    if humanoid then
        humanoid:TakeDamage(1)    -- 数字を大きくすると沢山ダメージを受けます
    end

イベントの解除

パーツそのものは消去せず、イベントだけを解除する方法です。

connection = script.Parent.Touched:Connect(function(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
 
    if humanoid then
        connection:Disconnect()    -- イベント解除
        humanoid:TakeDamage(50)
    end
end)

返信を残す