How to make while loop stop after you've taken 20 damage

Basically, what I thought I had was a script where after the player takes 20 damage, the fire stops and the player stops taking damage. But the break statement I added doesn’t do anything and the player takes damage infinitely. How do I fix this?

I want the fire and damage to stop after the player has taken 20 damage.

local firePart = script.Parent
local canTouch = true

local function onTouch(otherPart)
	local hum = otherPart.Parent:FindFirstChild("Humanoid")

	if hum and canTouch then
		canTouch = false
		local fire = Instance.new("Fire", otherPart)
		fire.Name = "Fire"
		fire.Enabled = true
		spawn(function()
			while fire and fire.Parent and hum and hum.Health > 0 do
				hum:TakeDamage(1)
				wait(0.5)
				if hum.Health == hum.Health - 20 then
					fire.Enabled = false
					hum:TakeDamage(0)
					break
				end
			end
		end)
		wait(1)
		canTouch = true
	end
end

firePart.Touched:Connect(onTouch)

If you have a solution, please let me know.

You should create a variable with the humanoid health in before the loop and then ever time the health decreases make it so the variable decreases or increases depending on how u wanna set it up.The issue with what you have is that your checking if the humanoid health is equals to the humanoid heath take 20 which makes no sense because the if statement will never be true so will never run.

If you want to check if the humanoid health is less then 20 just use something like::

if hum.Health <= 20 then

I tried that, but I get a blue line under my code

if hum.Health == hum.Health - 20 then

would always have a changing goalpost.

Maybe do

local startingHealth = hum.Health

Then

if hum.Health <= startingHealth - 20 then
1 Like

Under what part? I just tried it and it works fine for me.

That would not work. =< is not a vaild comparison operator, for what your trying to do it would be <=.

1 Like

Ahh I always get them confused

1 Like

I tried yours but it didn’t work

this is what I have now but it still doesnt stop the damage

local firePart = script.Parent
local canTouch = true

local function onTouch(otherPart)
	local hum = otherPart.Parent:FindFirstChild("Humanoid")

	if hum and canTouch then
		canTouch = false
		local fire = Instance.new("Fire", otherPart)
		fire.Name = "Fire"
		fire.Enabled = true
		spawn(function()
			while fire and fire.Parent and hum and hum.Health > 0 do
				hum:TakeDamage(1)
				wait(0.5)
				if hum.Health <= hum.Health-20 then
					fire.Enabled = false
					hum:TakeDamage(0)
					break
				end
			end
		end)
		wait(1)
		canTouch = true
	end
end

firePart.Touched:Connect(onTouch)

You did not do what I said. No need to have the hum.Health-20, just having 20 there after the comparison operator should work (if u wanna check if the health is under 20).

Try this

local firePart = script.Parent
local canTouch = true
local startingHealth = 0

local function onTouch(otherPart)
	local hum = otherPart.Parent:FindFirstChild("Humanoid")

	if hum and canTouch then
		canTouch = false
		local fire = Instance.new("Fire", otherPart)
		fire.Name = "Fire"
		fire.Enabled = true
        startingHealth = hum.Health
		spawn(function()
			while fire and fire.Parent and hum and hum.Health > 0 do
				hum:TakeDamage(1)
				wait(0.5)
				if hum.Health <= startingHealth-20 then
					fire.Enabled = false
					hum:TakeDamage(0)
					break
				end
			end
		end)
		wait(1)
		canTouch = true
	end
end

firePart.Touched:Connect(onTouch)
1 Like

i dont want the health to be under 20, I want the humanoid to take 20 points of damage before the fire stops

(i think we are thinking about different things but you may be right, just wanted to let you know)

Then follow what I said in the first message I sent (the first paragraph of the message).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.