Debouncing not working?

local debounce = false
	mouse.Button1Down:connect(function()
		if not debounce then
			debounce = true
			local target = mouse.Target
			if target then
				local targetBase = target.Parent.Parent.Parent
				if targetBase then
					print('yayp')
					if targetBase.Name == player.Name then
						print('player')
						if target.Parent.PrimaryPart.Name == 'HitBox' then
							--Make sure players can't destroy land purchases--
							print((mouse.Target).Parent)
						end
					end
				end
			end
			wait(0.45)
			debounce = false
		end
	end)

I have this print only when you click on a model that’s within your base, but when I do click on said model it will print everything twice, instead of just once.

1 Like

tested in studio and that code strictly (replacing missing stuff with mouse.Target prints or stuff) seems to work as intended
are you sure you run that sequence of code only once?

Are you testing this in studio or in a live server? The developer console in-game currently has a bug where it prints everything twice.

I recommend you look at the results of your code and not at the console when debugging, first and foremost.

1 Like

Your debounce is setup correctly so it will not run twice. You may have run this code multiple times resulting in two Button1Down events being connected which would explain why things are printed twice.

if you are usind a debounce in multiple places which are not shared it would be better to use a function which adds the debounce for you.

local function newDebounce(func, waitTime)
	local deb = false
	
	if waitTime then
		return function(...)
			if deb then return end
			deb = true
			
			func(...)
			wait(waitTime)
			deb = false
		end
	end
	
	return function(...)
		if deb then return end
		
		func(...)
		deb = false
	end
end

local mouse = game:GetService("Players").LocalPlayer:GetMouse()
-- with wait
mouse.Button1Down:Connect(newDebounce(function()
	print("with wait", mouse.Target)
end, 1))

-- without wait
mouse.Button1Down:Connect(newDebounce(function()
	print("without wait", mouse.Target)
end))

Could be the reason why.