Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't the debounce work at all?

I put debounce in my code, but it wont work and it gives the tool multiple times.

local buttonPressed = false

script.Parent.Touched:connect(function() 
if not buttonPressed then
	print("BPT")
	
	buttonPressed = true
	print("BPI")
	
game.Lighting.ClassicSlingshot:clone().Parent = game.Players.LocalPlayer.Backpack
print("Hi")

buttonPressed = false
print("BPF")

end
end) 

1 comment

0

I got it from the wiki

1 answer

✓ Accepted answer

Your script doesn't correctly define the debounce as it's supposed to be. The correct way would be to create an if statement making sure that you clarify if the variable is false, then you make it true. Once that variable is true, until it's false again it should not be able to work.

Example;

local variable= false
local part= script.Parent

part.Touched:Connect(function(hit)
		local hum= hit.Parent:FindFirstChild("Humanoid")
	if hum ~= nil and variable == false then variable = true
		hum:TakeDamage(20)
	end
		wait(5)
	variable = false
end)

I'll help you out by correcting your script as well, you can thank me later.

local buttonPressed = false

script.Parent.Touched:connect(function() 
	if buttonPressed == false then
   		buttonPressed = true
	game.Lighting.ClassicSlingshot:clone().Parent= game.Players.LocalPlayer.Backpack
	wait(5)
		buttonPressed = false
	end
end) 

This discussion is preserved as it was. New answers, comments, and votes are closed.