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

Is there a better method to use than this debounce, which seems unreliable?

Hi there! I have a collecting item script here that allows me to collect the item every 5 seconds (local waittime) with a debounce involved. However, the debounce seems to be unreliable, as sometimes it waits 5 seconds, other times it will spawn seemingly every .2 seconds. Is there a more reliable method of making this script actually wait 5 seconds every time?

It won't actually be every 5 seconds, it will be more like every 30+ seconds I just set it to 5 for testing purposes. It seems to work with no problems setting it to 20 seconds, but I also can't test it as often set to 20 seconds so maybe I just got lucky - or do debounces work better with higher wait times? I just want to make sure whatever I use, it's reliable and doesn't do anything funky as quickly as I found that glitch.

local CurrencyPickupEvent = game.ReplicatedStorage.CurrencyEvents.CurrencyPickup
local sound = script.Parent:FindFirstChild("Sound") --This is your sound
local waittime = 5 -- Time Between each hit
local amnt = 1 --how much you get for it
local db = false --the debounce

function onTouched(part)
    if not db then
        db = true --Turn on debounce
        local thisplr = game.Players:GetPlayerFromCharacter(part.Parent)
        if thisplr then
			CurrencyPickupEvent:Fire(thisplr, amnt)
            sound:Play(); --Play the sound
        	end
        end
        script.Parent.Transparency = 1
        wait(waittime)
        script.Parent.Transparency = 0 
        db = false --Turn off debounce
    end

script.Parent.Touched:connect(onTouched)

one idea: Should this script just be entirely disabled for x seconds and another script to re enable it after x seconds?

THANK YOU!

1 answer

Hi!

I believe I see the error in your script. When you touch the part but the db is true, it will still run the code past line 15. Meaning even though debounce is already on, it will still set is as false in 5 seconds. Here is the fixed version of your script.

local CurrencyPickupEvent = game.ReplicatedStorage.CurrencyEvents.CurrencyPickup
local sound = script.Parent:FindFirstChild("Sound") --This is your sound
local waittime = 5 -- Time Between each hit
local amnt = 1 --how much you get for it
local db = false --the debounce

function onTouched(part)
    if not db then
        db = true --Turn on debounce
        local thisplr = game.Players:GetPlayerFromCharacter(part.Parent)
        if thisplr then
            CurrencyPickupEvent:Fire(thisplr, amnt)
            sound:Play(); --Play the sound
        end
        script.Parent.Transparency = 1
        wait(waittime)
        script.Parent.Transparency = 0 
        db = false --Turn off debounce
    end
end

script.Parent.Touched:connect(onTouched)

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