Help with debounce on a Touched Event

Hi,

why do I get cash continous? I want a cooldown of 3 seconds. Only every 3 seconds should I get “Storage”. But it does random sometimes 5 secs nothing, somethings 10-20k per 3 secs.

3 ` local players=game:GetService(“Players”)
local active = false

script.Parent.TouchEnded:Connect(function()
active = false
end)

script.Parent.Touched:Connect(function(hit)
print(active)
active = true
if hit.Parent:FindFirstChild(“Humanoid”) ~= nil then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local leader = player:WaitForChild(“leaderstats”)
local freefolder = player:WaitForChild(“test”)
local free = freefolder:WaitForChild(“FreeStorage”)
local storage = leader:WaitForChild(“Storage”)
if leader and freefolder and free and storage then
while wait(5) do
if active == true and player.test.FreeStorage.Value >= 0 then
leader.Storage.Value = leader.Storage.Value + 500
end
end
end
end
end)

while wait(2) do
print(active)
end
3 `
Thanks!

Could you format the code by putting 3 ` before the code and after it? Also, you might need to use a debounce and add a wait(3)

Maybe try and replace your code with this:

local active = false

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        if not active then
            active = true
            player.leaderstats.Storage.Value += 500
            wait(3)
            active = false
        end
    end
end)
4 Likes

formatting dont work idk why look at the script :D. But the active value is the debounce or not? and there is a while wait loop

how can i formatting my code in devforum?

What you were trying here was a toggle not a debounce

So three of these “", like ```lua then put your script here, then close it off with the 3 "

lua test dfffddfdffdfddfdffdfdfd

It didn’t go through, Correction: `

So three of those, then lua then close it off

its formatting but there is written lua before

Then you can format it

local cool = true

if cool == true then
     print("you are cool")
end

k i understand thx ;:D. ffddddd

image