罗布洛克斯 Lua |如果角色重生,脚本将停止工作( HealthChanged )

Roblox Lua | If character respawns, script stops working ( HealthChanged )

基本上我正在制作一个脚本,当某个人死亡时打印 "played died" 虽然当这个人死亡时,脚本停止工作。这是来源:

local hum = game:GetService("Players").LocalPlayer.Character.Humanoid

hum.HealthChanged:connect(function(health)
if (health == 0) then
print("player died!")
end
end)

该脚本只能运行一次,当角色重生时如何让它再次运行?

Roblox 的 Humanoid class 有一个 "Died" 事件,请问您为什么不使用它?只要人形生物被斩首或生命值直接设置为 0,它就会触发。

在你的用法中,我会亲自尝试:

hum.Died:connect(function()
    print("A player has died!");
end)

他们在网站上提供的打印玩家姓名和死亡消息的脚本示例是:

game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            print(player.Name .. " has died!")
        end)
    end)
end)

以下是一些可能对您有用的链接:

http://wiki.roblox.com/index.php?title=API:Class_reference

http://wiki.roblox.com/index.php?title=API:Class/Humanoid

http://wiki.roblox.com/index.php?title=API:Class/Humanoid/Died