Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

fire clone breaks debounce?

Asked by 5 years ago
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

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

0
OP is living in 2008 while we are living in 2019 sahadeed 87 — 5y
Ad

Answer this question