Debounce not working

Hi, my debounce is not working correctly and I am not sure why. Anyone know? Continuously prints true

local debounce = true

regen.Touched:Connect(function(hit)
	if debounce then
		print("Touching ",debounce )
		local debounce = false
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if player then
			--code
		end
	end
	wait(5)
	debounce = true
end)
2 Likes
local debounce = true

regen.Touched:Connect(function(hit)
	if debounce then
		print("Touching ", debounce)
		debounce = false
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if player then
			--code
		end
	    task.wait(5)
	    debounce = true
	end
end)
3 Likes
local players = game:GetService("Players")

local debounce = true

regen.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") and debounce then
		debounce = false
		
		print(`Touching {debounce}`)

		local player = players:GetPlayerFromCharacter(hit.Parent)
		
		if player then
			--code
		end

		task.wait(5)
		
		debounce = true
	end
end)

Somehow that didn’t work. I added the task.wait(5) but touching keeps firing now with the error exception while signalling.

Add the whole code I replied with.

I have no idea what’s different compared to the change I made, but for some reason it works. Thanks!
–Oh wait I redefined it as local. lol that was a bit silly of me

1 Like

this is your code:

and this is their code:

they changed where the place of the wait and debounce = true

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