How can I make a debounce that runs within a certain time?

Well, basically the following script sends fire when a boolValue is true but when you press the key button instantly when the BoolValue is true, an error occurs and that is why I want to put a debounce on it so that it can be executed within a few milliseconds the script and therefore should not give any error

local UserInputService = game:GetService("UserInputService")
local Storage = game:GetService("ReplicatedStorage")

local Datos = Storage:FindFirstChild("DatosDeSistemaDeTala")
local Event = Datos:FindFirstChild("UnequipMaterialEvent")

local Player = game.Players.LocalPlayer

local character = Player.Character

local Rebote = false

if not character or not character.Parent then
	character = Player.CharacterAdded:wait()
end

UserInputService.InputBegan:Connect(function(KeyCode)
	local take = character.SistemaDeTala.Taken

	if take.Value == true then
		if not Rebote then
			Rebote = true
			if (KeyCode.KeyCode == Enum.KeyCode.Q) or (KeyCode.KeyCode == Enum.KeyCode.Backspace) then
				Event:FireServer(Player)
			end
			wait(0.4)
			Rebote = false
		end
	end
end)

Are you sure “take” is located on the character? If I insert a bool into my Player, and run the following local script it seemed to work fine. Or maybe the problem is server side?

local UserInputService = game:GetService("UserInputService")
local Storage = game:GetService("ReplicatedStorage")
local Datos = Storage:FindFirstChild("DatosDeSistemaDeTala")
local Event = Datos:FindFirstChild("UnequipMaterialEvent")
local Player = game.Players.LocalPlayer
local character = Player.Character
local taken = Player.Taken
local Rebote = false

local function onInputBegan(input, gameProcessed)
	if Rebote == false and taken.Value == true then
		Rebote = true
		if (input.KeyCode == Enum.KeyCode.Q) or (input.KeyCode == Enum.KeyCode.Backspace) then
			Rebote = true
			Event:FireServer(Player)
		end
	end
	wait(0.4)
	Rebote = false
end

UserInputService.InputBegan:Connect(onInputBegan)

The server side script was just…

local Players = game:GetService("Players")
local Storage = game:GetService("ReplicatedStorage")
local Datos = Storage:FindFirstChild("DatosDeSistemaDeTala")
local EventTry = Datos:FindFirstChild("UnequipMaterialEvent")

local function onEventTryFired(player)
	print(player.Name, "pressed Q or Backspace!")
end

EventTry.OnServerEvent:Connect(onEventTryFired)
1 Like