How would I add a cooldown for equipping/unequipping multiple tools

I have a gun system where when you equip/unequip the gun tool, requests are sent to the server to weld/unweld the gun from the character. However I’m having an issue where when players spam equip multiple gun tools, guns can sometimes stay welded on the player despite being unequipped. This occurs because the equipped function on the server takes a bit longer to run than the unequipped function, so while the equipped function is still being called first, the unequipped function gets called after but finishes running before the equipped function has actually welded the gun to the character, thus the gun appears to stay equipped.

How can I ensure that between multiple tools, there’s some sort of cooldown where the unequipped functions only get called after the equipped functions are finished running, and vice versa, ALSO accounting for when tools are switched and the unequipped and equipped functions get called practically instantaneously?

1 Like

have u tried using Debouce?

local Debounce = false

if not Debounce then
Debounce = true
– Code

– at the end of the code
wait(cooldown)
Debounce = false
end

A simple debounce will not work in this scenario because we are dealing with two independent events that are firing and two separate functions for every gun tool in the player’s backpack

then use remote.OnServerEvent:Wait()

Use a RemoteFunction/RemoteEvent or toggle a BoolValue which all the tools uses.

You can try this (cooldown only for equipping one tool):

local canEquip = true
local function toolCooldown()
	if canEquip then
		canEquip = false
		wait(5) -- Wait 5 seconds before being able to use the tool
		canEquip = true
	end
end

tool.Equipped:Connect(function()
	if not canEquip then
		tool.Parent = backpack -- If it's too early to equip the tool, move it back to player's backpack
	end
	
	-- Tool equipped scripting here
end)

tool.Unequipped:Connect(function()
	spawn(toolCooldown) -- Start the cooldown
	
	-- Tool unequipped scripting here
end)