Debounce not working

There may be a solution to this and I’m just really blind, but I don’t see the problem in my script. I made a quick script that made the player sit down when they press a button. I made it so you can’t use it after 10 seconds, but when I tested it in my game, I was able to spam it.

script (PC and Xbox version):

local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")

local Tapped = false
UserInputService.InputBegan:Connect(function(Input, GameStuff)
	if GameStuff then return end
		if Input.KeyCode == Enum.KeyCode.F or Input.KeyCode == Enum.KeyCode.ButtonL1 then
	local db = false

	if db == false then
		db = true
		
		local player = game.Players.LocalPlayer

local char = player.Character
		char.Humanoid.Sit = true
		

		end
		wait(10)
		db = false

		end
end)

script (Mobile version):

local db = false


script.Parent.MouseButton1Click:Connect(function()
	if db == false then
		db = true
		
		local player = game.Players.LocalPlayer

local char = player.Character
		char.Humanoid.Sit = true

	end
	wait(10)
		db = false
	
end)

I honestly don’t know what’s wrong with it

2 Likes

try to put the debounce variable outside the function

Try doing Debounce instead of DB.

that sadly didn’t work. I could still spam it

debounce is just what the community calls it. It can actually be renamed to anything, so naming it to debounce won’t do anything

yea you are right about this, but it is good practice to make your variables actual words

that is because of the definition of debounce

but is there any way to fix my debounce issue?

First of all, you should put the “debounce = false” out of the function.
Then you should try to make the “if” statements check for the variable AND if the debounce is FALSE in order to execute the code
Basically checking if multiple conditions are met.

PC fix:

local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")

local db = false
local Tapped = false
UserInputService.InputBegan:Connect(function(Input, GameStuff)
	if GameStuff then return end
	if Input.KeyCode == Enum.KeyCode.F or Input.KeyCode == Enum.KeyCode.ButtonL1 then
		if db == false then
			db = true
			local player = game.Players.LocalPlayer
			local char = player.Character
			char.Humanoid.Sit = true
		wait(10)
		db = false
		end
	end
end)

Here, we move the variable local debounce outside of the function since it would reset back to false if you pressed the button once again. We then moved the wait() function including the db = false inside the if statement to avoid spam, making it work as it’s intended to work.

Mobile Fix:

local db = false


script.Parent.MouseButton1Click:Connect(function()
	if db == false then
		db = true
		local player = game.Players.LocalPlayer
		local char = player.Character
		char.Humanoid.Sit = true
		wait(10)
		db = false
	end
end)

Here, you were able to spam click and the wait() function would run as many times as you clicked, and would make the debounce become false, even if it was already, or the debounce just turned true. So that’s why it was added inside the if statement.

Post came up after a long while, that’s a very late response, you probably have it all fixed by now, if not, I hope this helped you.

I kinda just got used to the problem and decided not to fix it, but thanks!

1 Like

You’re welcome, you should also move the local Player and local char outside of the function as they are not really needed to be assigned all the times that you click your mouse button. Not doing this, I don’t believe it will hurt performance, just some additional details.

1 Like

I ran into a similar issue while trying to script a sitting button. I set up the user input service to the E key and made the coded the rest of the script to make my character sit. It didn’t work though. However that’s until I learned what remote events were. I made my own version of your coding just now. try using a remote event if your coding doesn’t work.