Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

fire clone breaks debounce?

function onTouched(hit)
	local debounce = false
        local human = hit.Parent:findFirstChild("Humanoid")
		local genfire = script.Parent.Fire:clone()
        if (human ~= nil) and debounce == false then
		debounce = true
		genfire.Parent = human.Parent:findFirstChild("Torso")
		wait(5)
		genfire:Destroy()
		wait(10)
		debounce = false
        end
end
script.Parent.Touched:connect(onTouched)

genfire is a variable and i am destroying it after five seconds, but i tested this and the debounce broke

1 answer

✓ Accepted answer

The issue is that a new debounce variable is declared each time the part is touched.

So it's "local" to that one touch and not "global".

What you should be doing is declaring it at the top so the function writes to and reads the same variable each time and not a new one each time

local debounce = false

script.Parent.Touched:Connect(function(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    
    if humanoid and not debounce then
        debounce = true
        local genfire = script.Parent.Fire:Clone()
        genfire.Parent = humanoid.RootPart -- RootPart is a property of humanoid
        wait(5)
        genfire:Destroy()
        wait(10)
        debounce = false
    end
end)

I also cleaned up the code and removed deprecated items, like :clone, :findFirstChild, :connect which are deprecated in favour of the uppercase versions.

1 comment

0

OP is living in 2008 while we are living in 2019

This discussion is preserved as it was. New answers, comments, and votes are closed.