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