Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can you fix this script?

Asked by 10 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

So I made a Bool value, and I want it to go false when a player dies, but it's not going well with me. Can you help? I'd appreciate it if you can!

h = script.Parent:FindFirstChild("Humanoid")
player = h:GetPlayerFromCharacter(script.Parent.Parent)
x = player:FindFirstChild("Ingame")

if h.Health == 0 then
    x.Value = false 
end

1 answer

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

Humanoid.Health is a floating-point value, (aka a Number), so it will most likely never be exactly 0. Also, there exists a Died event for Humanoids, so you don't have to check the health at all. GetPlayerFromCharacter is a method of Players, not a Humanoid.

Really, this could be better done using the PlayerAdded and CharacterAdded Events, so I'll give you a fixed version of your code, and a version using those two:

h = script.Parent:FindFirstChild("Humanoid")
if h then
    player = Game.Players:GetPlayerFromCharacter(script.Parent.Parent)
    if player then
        x = player:WaitForChild("Ingame") --Safer than FindFirstChild, will yield the thread until Ingame exists.

        h.Died:connect(function()
            x.Value = false
        end)
    end
end
Game.Players.PlayerAdded:connect(function(player)
    local x = player:WaitForChild("Ingame")
    player.CharacterAdded:connect(function(character)
        wait() --Wait for the character to fully load.
        character.Humanoid.Died:connect(function()
            x.Value = false
        end)
    end)
end)
2
The statement about "never exactly zero" is not really accurate. Humanoids cannot have negative health, so TakeDamage or even subtracting from Health directly will very likely result in exactly 0, as well as environment deaths like falling or being broken apart. BlueTaslem 18071 — 10y
0
Thanks for the advice! chill22518 145 — 10y
Ad

Answer this question