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

Trying to run a script when the Humanoid of the script's parent reaches 0?

I'm trying to loop the code endlessly by using a while true do. Sometimes, when I change the Humanoid's property Health to zero using the Properties, the script works. However if I set my health to a higher number, and the health is drained using another script, until it reaches 0, nothing happens.


while true do
if script.Parent.Humanoid.Health == 0
	then local a = Instance.new("Message")
	a.Text = "The base has been destroyed. You have failed."
	a.Parent = game.Workspace
	wait (4)
	a:remove()
end
end

I have a separate script in the same model "Base", to give the Humanoid more than 100 health. Here is that script. It's called HumanoidHealth

script.Parent.Humanoid.Health = 10
wait (1) 
script.Parent.HumanoidHealth:Destroy()

2 answers

Another way I do it is like this

script.Parent.Humanoid.Changed:connect(function()
if script.Parent.Humanoid.Health==0 then
local a = Instance.new("Message", workspace)
a.Text = "The base has been destroyed. You have failed."
game.Debris:AddItem(a, 4)
end
end)

This way it doesn't constantly check. It will check only when the Humanoid has been changed (Maybe the Health)

1 comment

0

That works as well. I actually think your method was better than mine. I just tried to correct his doe. GG

You added a 4 second wait, that will slow things down alot. Debris is EXTREMELY useful when it comes to waiting to remove things in loops.

while wait() do
if script.Parent.Humanoid.Health == 0 then
local a = Instance.new("Message", game.Workspace)
a.Text = "The base has been destroyed. You have failed."
game.Debris:AddItem(a, 4)
end
end

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