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

How will I add a debounce?

Asked by 7 years ago

I want to prevent this script from running more than once, so I figured if I use a debounce, that will fix my problem, but I don't know where...


script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then script.Parent.Sound:Play() local player = game.Players.LocalPlayer player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1 wait() script.Disabled = true script.Parent:Destroy() else wait(10) script.Parent:Destroy() end end)

1 answer

Log in to vote
1
Answered by 7 years ago
local touched = false -- define your debounce

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    if touched == false then
        touched = true
        script.Parent.Sound:Play()
        local player = game.Players.LocalPlayer
        player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
        wait()
        script.Disabled = true
        script.Parent:Destroy()
        touched = false -- make it so that players can touch it again
    else
        wait(10)
        script.Parent:Destroy()
        touched = false
    end
    end
end)

Hope this helped you! I might not have solved it all the way but I think this will help.

Ad

Answer this question