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

script keeps running multiple times even with debounce?

Asked by 4 years ago
local debounce = false
local debounce2 = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and not debounce then
        debounce = true
        print("debounce active")
        local char = hit.Parent
        print(char.Name)
        local chardata = game.ServerScriptService.DataCore:FindFirstChild(char.Name)
        if chardata and not debounce2 then
            debounce2 = true
            print("giving gems to"..char.Name)
            chardata.Gems.Value = chardata.Gems.Value + 10
            print("gave 10 gems to"..char.Name)
            wait()
            script.Parent.Parent:Destroy()
        end
    end

end)

this script adds 10 "gems" to a folder owned by the player. but for some reason it keeps running more than once resulting in 30 or 40 gems being added. I added a second debounce to try and prevent it from happening but regardless it continues to be a problem.

the script does fully run, but it runs fully more than once.

1 answer

Log in to vote
2
Answered by 4 years ago

model ur code like this:

local debounce = false
local dbLength = 3 -- waits 3 seconds
script.Parent.Touched:Connect(function(hit)
    if not debounce then
        debounce = true
        --put the rest of the code here
        --   ...

        wait(dbLength )
        debounce = false
    else
        print("debounce is true")
        --code  to run if debounce is true
    end
end)

that should work, good luck!

Ad

Answer this question