Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can you fix this script?

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

✓ Accepted answer

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 comments

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.

0

Thanks for the advice!

This discussion is preserved as it was. New answers, comments, and votes are closed.