Roblox Lua | 如果角色重新生成,脚本就停止工作(HealthChanged)

基本上我正在制作一个脚本,当某个人物死亡时打印出“玩家死亡”,但当该人物死亡时,脚本会停止工作。以下是源代码:

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

hum.HealthChanged:connect(function(health)
if (health == 0) then
print("玩家死亡!")
end
end)

脚本只能执行一次,如何使它在角色重新生成时再次执行?

原文链接 https://stackoverflow.com/questions/51041889

点赞
stackoverflow用户10018042
stackoverflow用户10018042

Roblox的Humanoid类有一个“Died”事件,我想问问为什么你不使用它?当人形被斩首或直接设置为0血时,它会触发。

在你的使用中,我个人会尝试:

hum.Died:connect(function()
    print("一个玩家已经死亡!");
end)

他们在其网站上提供的一个脚本示例,打印出玩家的名称以及死亡信息是:

game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            print(player.Name .. "已经死亡!")
        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

2018-07-01 15:58:00