function hurt(hit) local fire = hit.Parent.Torso:FindFirstChild("Fire") local hp = hit.Parent:FindFirstChild("Humanoid") while fire do hp.Health = hp.Health -5 wait(1) end end lava.Touched:connect(hurt)
I'm trying to make it so that if fire is on the player the player takes damage and if it isn't the while loop stops. However, I dunno how to stop it.
I've tried this as well
function hurt(hit) local nofire = hit.Parent.Torso local fire = hit.Parent.Torso:FindFirstChild("Fire") local hp = hit.Parent:FindFirstChild("Humanoid") if fire then while true do hp.Health = hp.Health -5 wait(1) if nofire then hp.Health = hp.Health +0 end end end end lava.Touched:connect(hurt)
The problem with this is that your variable fire
is static. When you define it, it will always be true or false. You need to use the FindFirstChild function inside the definition of the while loop.
function hurt(hit) local hp = hit.Parent:FindFirstChild("Humanoid") while hit.Parent.Torso:FindFirstChild("Fire") and wait(1) do hp.Health = hp.Health - 5 end end lava.Touched:Connect(hurt) --'connect is deprecated - Use Connect.
Assuming there is a Fire
Instance inside the Torso, the script will continue to damage the player every second.