Problem:
I wanted to know if there was a way I could make a cooldown that the player has to wait After Unequipping a sword. So basically after they unequip a tool they have to wait X amount of seconds in order to be able to equip it again.
Problem:
I wanted to know if there was a way I could make a cooldown that the player has to wait After Unequipping a sword. So basically after they unequip a tool they have to wait X amount of seconds in order to be able to equip it again.
You can use debounce?
local debounce = os.clock()
local cooldown = 3 -- seconds
Tool.Unequipped:Connect(function() debounce = os.clock() end)
Tool.Equipped:Connect(function()
if os.clock() - debounce < cooldown then return end
-- code goes here.
end)
So this is just a bit of a hacky solution… It works but a better approach would likely be to implement a system which manually equips Tools using functions of the Humanoid like EquipTool.
(code goes in Script not LocalScript as direct child of the Tool)
local cooldown = false
script.Parent.Equipped:connect(function()
if cooldown then
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
if player then
wait() --Required or otherwise a warning will be triggered about "Setting the Parent of Tool while something else was trying to set its Parent" and the Tool will not be unequipped
script.Parent.Parent = player.Backpack
else
error("Failed to located Player")
end
end
end)
script.Parent.Unequipped:connect(function()
if not cooldown then
cooldown = true
wait(5)
cooldown = false
end
end)
Roblox’s wait()
is usually a bad practice, you should only check the cooldown when the player equip the tool
What? What’s wrong with wait() and the check in the Unequipped function is necessary to ensure that multiple threads don’t get started for the cooldown debounce.
I just feel like instead of a normal Roblox’s wait()
, you should use RunService.Heartbeat:Wait()
instead
Developer pages say RunService.Heartbeat is deprecated. Not sure if it’s not up to date or something, but I’ve never really been a fan of Heartbeat. I know it has its use cases but I’ve never had an issue with wait calls being delayed for too long. In this case, I don’t see it causing too big of a problem even if it does.
Works Like a Charm! thanks so much
ill try your solution right away!
Hope this helps!
Only use this if the script is a child of a tool.
local debounce = false
local cd = 3 -- Change to desired amount (seconds)
Tool.Unequipped:Connect(function()
if debounce == false then
debounce = true
wait(cd)
debounce = false
end
end)
Tool.Equipped:Connect(function()
if debounce == true then
local char = script.Parent.Parent -- assuming the script is a child of the tool
char.Humanoid:UnequipTools()
end
end)
The Script did not work but, I really appreciate you taking your time to write this script:)
hmmm Im not sure why it dosent work xD I wrote that in the website and since u already got the answer from the others I’ll just not test it in studio.