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

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

Asked by 10 years ago

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

Log in to vote
1
Answered by
SirGory 15
10 years ago

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)

0
That works as well. I actually think your method was better than mine. I just tried to correct his doe. GG bobafett3544 198 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

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

Answer this question