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)
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::
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).
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)