Why won't .Touched event wait until firing again?

While walking on it, it keeps firing again and the sound keeps playing. I only want it to play once, then pause for 3 seconds. How can I fix the code?

local debounce = false
script.Parent.Essentials.Giver.Touched:connect(function(hit)
	if not debounce then
       remoFun:FireClient(player) --play chaching money sound
	   debounce = true
    end
   wait(3)
   debounce = false
if not debounce then
       remoFun:FireClient(player) --play chaching money sound
	   debounce = true
wait(3)
   debounce = false
    end
2 Likes

you’ve must put this line below into if then statement:

wait(3)
debounce = false

so it will be:

local debounce = false
script.Parent.Essentials.Giver.Touched:connect(function(hit)
	if not debounce then
		remoFun:FireClient(player) --play chaching money sound
		debounce = true
		wait(3)
		debounce = false
	end
end)
1 Like

I would also recommend putting debounce = true before remoFun:FireClient(player) to ensure that it won’t run again while the remote function is being fired.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.