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

How will I add a debounce?

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

✓ Accepted answer
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.

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