Debounce Issues

For some reason this debounce isn’t working:

local disabled = true
local FireMagic = game.ReplicatedStorage.Activations:WaitForChild("FireMagic")

FireMagic.OnClientEvent:Connect(function()
    disabled = false
    print("fired")
end)

if disabled then return end

The print(“fired”) function works which means that disabled is set to false, but it still prevents the script below it from working.

local disabled = true
local FireMagic = game.ReplicatedStorage.Activations:WaitForChild("FireMagic")

FireMagic.OnClientEvent:Connect(function()
    if disabled then
       disabled = false
       print("fired")
       task.wait(2)
       disabled = true
    end
end)

Is that what you’re trying to get?

1 Like

It seems like you are trying to wait for the fire magic to happen for the code to run. Instead, move the code below the if disabled then return end into the onClientEvent.

local FireMagic = game.ReplicatedStorage.Activations:WaitForChild("FireMagic")

FireMagic.OnClientEvent:Connect(function()
    print("fired")
    --code here
end)

Thanks again Kaiden, your solution does work. Still, if I were to use debounces, do you know how would the script be written out?

The debounce is a interval between doing something and it bouncing back. So you will have your

disabled = true -- before the debounce 
wait(number here) -- The interval before the debounce bounces back

And then

disabled = true -- What the debounce bounces back

Yes thanks, I have scripted other debounces before for cooldowns. But right now I’m trying to use it to disable / activate a script.

Yeah, that’s what there most commonly and easily used for.

What do you mean?! This is scripting support it has nothing to do with DevEx

The script would be written almost the same exact way, just moving some things and adding a few lines.

local debounce = false
local FireMagic = game.ReplicatedStorage.Activations:WaitForChild("FireMagic")

FireMagic.OnClientEvent:Connect(function()
    if debounce then return end
    debounce = true
    
    task.delay(2, function() --2 represents the wait in seconds
        debounce = false --will run this after the wait
    end) --Kinda works like task.spawn, doesn't interrupt the code.
    
    print("fired")
    --code here
end)