I’m trying to create a global cooldown where after I cast a magic move, the rest of the other moves go in cooldown for a short period. I’m currently planning to create that cooldown by disconnecting and reconnecting the moves after a remote event has been fired, but for some reason when I used the connect function after the disconnect function, it doesn’t work and an error message pops up (“Connect is not a valid member of RBXScriptConnection”).
I tried using debounce but it also doesn’t work for some reason.
Here’s my code:
local debounce = false
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local tool = script.Parent
local activated
local Gmc = game.ReplicatedStorage:WaitForChild("GlobalMoveCd")
local function act()
local function cd()
tool.Name = "X"
act:Disconnect()
wait(2)
act:Connect()
tool.Name = "Fire Charge"
end
Gmc.OnClientEvent:Connect(cd) -- The part where I try to add the global cooldown
local cooldown = 2
tool.Equipped:Connect(function()
activated = Mouse.Button1Down:Connect(function()
if debounce then return end
game.ReplicatedStorage.Fire.FireChargeEvent:FireServer()
debounce = true
local function timedisplay()
local cdtime = cooldown
for count = 1,cooldown,1 do
tool.Name = cdtime
cdtime = cdtime - 1
wait(1)
if cdtime == 0 then
tool.Name = "Fire Charge"
end
end
end
timedisplay()
debounce = false
end)
end)
tool.Unequipped:Connect(function()
activated:Disconnect()
end)
end
act()
When you disconnect a function, you can no longer use it. I recommend adding a Boolean for your case and change the Boolean value to true if they used a magic move and reset the Boolean after a short period of time.
Use this:
local deb = true
local cooldown = 10 -- Seconds
function run()
if deb == true then
deb = false
-- Cooldown is over, can be used again
-- Code goes here
wait(cooldown)
deb = true
else
-- Cooldown is still there, can't be used yet
-- Code goes here
end
end
Please mark this as solution if you think it satisfied your expectations!
local deb = true
local cooldown = 10 -- Seconds
function run()
if deb == true then
deb = false
-- Cooldown is over, can be used again
-- Code goes here
wait(cooldown)
deb = true
else
-- Cooldown is still there, can't be used yet
-- Code goes here
end
end
Thanks, but I tried using the debounce earlier and it doesn’t work. I’m not sure why but my script is in a local script where it connects the mouse input to the move by firing a remote event when I click.
local tool=script.Parent
local cooldown=2
local activated=nil
local event=game.ReplicatedStorage.Fire.FireChargeEvent --event
local debounce=false
local function timeDisplay()
for count=cooldown,0,-1 do
tool.Name=count
task.wait(1)
if count==0 then
tool.Name="Fire Charge"
end
end
debounce=false
end
tool.Activated:Connect(function()
if debounce then return end
debounce=true
event:FireServer()
end)
event.OnClientEvent:Connect(function(cooldown)
timeDisplay()
end)
tool.Unequipped:Connect(function()
if activated==nil then return end
activated:Disconnect()
end)
This is unfortunately wrong. You can still use a function after it is disconnected.
local function myFunction()
-- code
end
local myConnection = Event:Connect(myFunction)
wait(60)
myConnection:Disconnect()
myFunction()
-- it's possible to reconnect too as long as you have your logic correctly
myConnection = Event:Connect(someOtherFunction)
In your case you will want to review your logic.
First, this is called a closure. They are used when you want to segregate variables or functions which refer to the same function but with different values.
They are quite handy however using them on a large scale for no reason is a waste of memory as each closure with it’s variables will use a lot of memory.
It’s best to keep this all event based. I recommend using this boilerplate I created a while ago. I use it for tools it’s quite nice as there is only 1 event.
local Tool: Tool = script.Parent
local Player = Players.LocalPlayer
-- Variable
local LMB_Connection: RBXScriptConnection = nil
local RMB_Connection: RBXScriptConnection = nil
-- Events
Tool.Equipped:Connect(function(mouse: Mouse)-- Connect events after equip
LMB_Connection = mouse.Button1Down:Connect(function()
local Origin: CFrame = mouse.Origin
mouse.Button1Up:Wait()
end)
RMB_Connection = mouse.Button2Down:Connect(function()
mouse.Button2Up:Wait()
end)
Tool.Unequipped:Wait() -- Disconnect events after unequip
LMB_Connection:Disconnect()
RMB_Connection:Disconnect()
end)
If you would like your functions implemented as variables then this will too work.
local Tool: Tool = script.Parent
local Player = Players.LocalPlayer
-- Variable
local LMB_Connection: RBXScriptConnection = nil
local RMB_Connection: RBXScriptConnection = nil
-- Functions
local function LMB_Down()
end
local function RMB_Down()
end
-- Events
Tool.Equipped:Connect(function(mouse: Mouse)-- Connect events after equip
LMB_Connection = mouse.Button1Down:Connect(LMB_Down)
RMB_Connection = mouse.Button2Down:Connect(RMB_Down)
Tool.Unequipped:Wait() -- Disconnect events after unequip
LMB_Connection:Disconnect()
RMB_Connection:Disconnect()
end)
I highly recommend the first way as you keep your arguments.
Thank you, but just to clarify, for the Reconnect function script, is the ‘Event’ a placeholder for something else? I tried fitting the script in but there’s an error that says “Unknown global ‘Event’.” (I’m not very good at scripting).