Is there a way to reconnect a function after disconnecting it?

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()
3 Likes

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!

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
1 Like

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.

May I see the full code? Maybe then I can tell what’s wrong.

@I_lFighter
what do you connect/disconnect
how did you do it? i n the script?!

Maybe instead of adding a debounce inside the script add it inside the player, also can I see your code?

Sure I’ll add my code to the topic

I think I know the problem, when you connect the mouse input do you put the debounce inside the connection?

You’ve just copied @dutycall11’s suggestion …

Also, both of you should use task.wait() instead of wait().
And instead of if deb == true then, simply do if deb then

I didn’t, check how long ago both were posted.

Yes don’t know why they copied my suggestion lol maybe it was a coincidence

He copied mine. (this is for the chars)

??? False assumption
char limit

Ayo what i swear i remember typing that code, anyways still didn’t work for him

Anyways I guess it doesn’t matter who did what, it only matters that the debounce script didn’t work

local script
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)
Server Script
game.ReplicatedStorage.Fire.FireChargeEvent.OnServerEvent:Connect(function(plr)
	--fire
	
	game.ReplicatedStorage.Fire.FireChargeEvent:FireClient(plr)	
	
end)

something like this should work

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.

4 Likes

One more clarification. Functions are not disconnected. Only events can be connected and disconnected.

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).