The fire damage keeps stacking up over time

I am creating a fire status script, and the damage keeps stacking up over each time the player is hurt.

I know the problem is that the loops stack up on each time the script gets changed, but I do not know how to fix this, so I am looking for someone to give a hand on this.

local head = script.Parent:FindFirstChild("Head")

local fca = head:FindFirstChild("FaceCenterAttachment")

local burningstat = script:FindFirstChild("BurnTimer")

local hum = script.Parent:FindFirstChildOfClass("Humanoid")

burningstat.Changed:Connect(function()
	if burningstat.Value >= 0 then
		print("Thing ran!")
		local fire = game.ReplicatedStorage.Fire:Clone()
		fire.Parent = fca
		while burningstat.Value >= 0 do
			print("Ouch!")
			wait(1)
			hum:TakeDamage(5)
			burningstat.Value = burningstat.Value - 1
		end
	elseif burningstat.Value <= 0 then
		fca:Destroy()
	end
end)

If there is anything I need to change, Fell free to tell me.

local head = script.Parent:FindFirstChild("Head")

local fca = head:FindFirstChild("FaceCenterAttachment")

local burningstat = script:FindFirstChild("BurnTimer")

local hum = script.Parent:FindFirstChildOfClass("Humanoid")
local fireActive = false

burningstat.Changed:Connect(function()
	if burningstat.Value >= 0 and not active then
        active = true    -- Activate here
		print("Thing ran!")
		local fire = game.ReplicatedStorage.Fire:Clone()
		fire.Parent = fca
		while burningstat.Value >= 0 do
			print("Ouch!")
			wait(1)
			hum:TakeDamage(5)
			burningstat.Value = burningstat.Value - 1
            
		end
        active = false  --Deactivate here
	elseif burningstat.Value <= 0 then
		fca:Destroy()
	end
end)

You could probably try making and active variable to check whether or not the fire is already active.

2 Likes
local head = script.Parent:FindFirstChild("Head")
local fca = head:FindFirstChild("FaceCenterAttachment")
local burningstat = script:FindFirstChild("BurnTimer")
local hum = script.Parent:FindFirstChildOfClass("Humanoid")

local function burn(length)
    local fire = game.ReplicatedStorage.Fire:Clone()
    fire.Parent = fca
    for i = 0, length, 1 do
        hum:TakeDamage(5)
        wait(1)
    end
    fca:Destroy()
end

What I just wrote is a function you can call to deal fire damage for x amount of time (the “length” parameter is amount of time in seconds)
So instead of what you have currently you can instead make it so when you want the player to get dealt fire damage you can call this function instead. Hope this helps please let me know if I made some accidently errors in my code as I don’t have an editor on me at the moment.

I think that solution might be better than mine lol

Where do you run the function?

You run the function anywhere you have it accessible. In your case if you want the function to run when you touch a part, you put a script in the part and then can do the following,
Example:

local function burn(length, fca)
    local fire = game.ReplicatedStorage.Fire:Clone()
    fire.Parent = fca
    local hum = fca.Parent.Parent:FindFirstChild("Humanoid")
    for i = 0, length, 1 do
        hum:TakeDamage(5)
        wait(1)
    end
    fca:Destroy()
end

script.Parent.Touched:Connect(function(hit) -- script.Parent would be the part you put the script in
    if hit.Parent:FindFirstChild("Humanoid") then -- the hit parameter has a humanoid meaning it's a player
        local head = script.Parent:FindFirstChild("Head")
        local FaceCenterAttachment = head:FindFirstChild("FaceCenterAttachment")
        burn(5, FaceCenterAttachment )
    end
end)

Please note I modified the code to accommodate the part-touching functionality

I changed up your code a bit, and I figured it out, thanks a plenty!

1 Like

I think the solution is to add debounce to the function. Here’s what the script should look like:

local head = script.Parent:FindFirstChild("Head")

local fca = head:FindFirstChild("FaceCenterAttachment")

local burningstat = script:FindFirstChild("BurnTimer")

local hum = script.Parent:FindFirstChildOfClass("Humanoid")
local fireActive = false
local debounce = true

burningstat.Changed:Connect(function()
    if debounce == true then
             debounce = false
             if burningstat.Value >= 0 and not active then
             active = true    -- Activate here
	         print("Thing ran!")
	         local fire = game.ReplicatedStorage.Fire:Clone()
	         fire.Parent = fca
	         while burningstat.Value >= 0 do
		         print("Ouch!")
		         hum:TakeDamage(5)
		         burningstat.Value = burningstat.Value - 1
        
	         end
             active = false  --Deactivate here
         elseif burningstat.Value <= 0 then
	         fca:Destroy()
         end
         wait(1)
         debounce = true
     end
end)

My question has already been solved, but thank you for contributing!

1 Like