Why aren't my functions disconnecting?

I have two functions that should disconnect when a criteria is met, I have a characterAdded event that should only run while the functions are connected but for some reason thats not the case since when I reset after they should have disconnecteed the characteradded function still runs. Any ideas why?

local connections = {}
local function disconnectConnections(kills, loserPlayer)
	if kills == 5 then
		for i, v in ipairs(connections) do
			v:Disconnect()
		end
connections[1] = player1.CharacterRemoving:Connect(function(hit)

				char1 = nil
				char2 = nil

				char1 = player1.Character
				char2 = player2.Character

				local player1 = game.Players:GetPlayerFromCharacter(char1)

				local humanoid1 = char1.Humanoid

				plr2CurrentKills = plr2CurrentKills + 1

				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

				if disconnectConnections(plr2CurrentKills, player1) then return end -- will check the kills and disconnect connections if it's over 5
				
				player1.CharacterAdded:Connect(function(chr) -- THIS IS RUNNING AFTER THEY BEEN DISCONNECTED WHEN IT SHOULD'T
					task.wait(0.5)
					if plr1CurrentKills == 5 or plr2CurrentKills == 5 then
						print("Nope")
					else
						if chr:FindFirstChild("ForceField") then  
							print("RAHHHHHHHHHHHHHH")
							chr:FindFirstChild("ForceField"):Destroy()
							chr.Humanoid.WalkSpeed = 16
						end
					end
				end)
3 Likes

The player1 variable inside the CharacterAdded event is being redefined after disconnection, causing it to refer to a different player object. Ensure player1 remains consistent or use a different approach to reference the correct player object.

3 Likes

Sorry if i’m being stupid but surely if the player1 variable is being redefined then the character added event won’t run (which is what i’m trying to get).

Ah ah, it’s alright. Sometimes I make mistakes too. There is no shame.

1 Like

Sorry i worded that weirdly, it was a question. How does me redefining the variable after they have been disconnected affect whether the characterAdded event is run?

Personally, it’s likely due to the events not being disconnected properly or being reconnected elsewhere in your code. But I don’t really find much solutions, I do my best.

1 Like

You didn’t assign this connection to any variable, how are you disconnecting it?

image

1 Like

He returns the function.

image

but the function with the open connection(the initial run) is still running

i’m thinking he needs another connections[whatever] = for this one that can be disconnected

I found a little forum for this problem, I don’t really know if it fixes the issue. Maybe return end isn’t really usefull for it.

image

Because return end is for returning a value, but it doesn’t really affect the function.

1 Like

‘return end’ is only stopping a new connection from being created, its not stopping the old one that was already created.

1 Like

return end is not really precise to be honest. So all I can say is that Disconnect() is my only solution for this problem.

1 Like
local function disconnectConnections(kills, loserPlayer)
	if kills == 5 then
		for i, v in ipairs(connections) do
			v:Disconnect()
		end

Does this not disconnect them?

Two connections are being added here. It’s no surprise, since one connection cutting does not induce another disconnection of another. Therefore, the solution is simple as adding the next connection into the table.

The alternate only funny way that both connections become disconnected is when the Player instance is deleted…

1 Like

The ‘problem’ connection is never added to this table that you are looping through.

this one:
image

1 Like

It might be that by using connections[1] it’s accidentally overwriting an existing connection that’s already located in index 1, which would cause that connection to never be disconnected. I recommend using this method instead:

table.insert(connections, player1.CharacterRemoving:Connect(function(hit)

Good idea, but he will still need to insert the other connection that he didn’t save to any variable.

1 Like

That’s correct :grin:

Luckily using the table.insert method makes it convenient to fix, so here’s how the finished code will look like:

table.insert(connections, player1.CharacterRemoving:Connect(function(hit)

				char1 = nil
				char2 = nil

				char1 = player1.Character
				char2 = player2.Character

				local player1 = game.Players:GetPlayerFromCharacter(char1)

				local humanoid1 = char1.Humanoid

				plr2CurrentKills = plr2CurrentKills + 1

				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Sword Fighting Arenas"].Arena1.Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

				if disconnectConnections(plr2CurrentKills, player1) then return end -- will check the kills and disconnect connections if it's over 5
				
				table.insert(connections, player1.CharacterAdded:Connect(function(chr) -- THIS IS RUNNING AFTER THEY BEEN DISCONNECTED WHEN IT SHOULD'T
					task.wait(0.5)
					if plr1CurrentKills == 5 or plr2CurrentKills == 5 then
						print("Nope")
					else
						if chr:FindFirstChild("ForceField") then  
							print("RAHHHHHHHHHHHHHH")
							chr:FindFirstChild("ForceField"):Destroy()
							chr.Humanoid.WalkSpeed = 16
						end
					end
				end))

local function disconnectConnections(kills, loserPlayer)
	if kills == 5 then
		for i, v in ipairs(connections) do
			v:Disconnect()
		end
		table.clear(connections)
2 Likes

Thank you so much it’s working perfectly. Thank you everyone else that helped too! :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.