Disconnecting all connections?

Hello everyone,

So recently, I’ve been making a neat round-system and I’ve came across a big error.

The error is, when I try disconnecting a function, it only works for one player.

What I have tried is, creating a table, then looping through the table and then disconnecting. Now, for some reason It doesn’t seem to work. Maybe I did it wrong?

Here is the bit that’s the main cause:

			deathfunction = player.CharacterAdded:Connect(function(char)
				local random_melee = meleefolder[math.random(1,#meleefolder)]:Clone()
				local random_ranged = rangedfolder[math.random(1,#rangedfolder)]:Clone()
				local random_special = specialfolder[math.random(1,#specialfolder)]:Clone()
				random_melee.Parent = player.Backpack
				random_ranged.Parent = player.Backpack
				random_special.Parent = player.Backpack

				repeat task.wait() until char:IsDescendantOf(workspace)
				char.HumanoidRootPart.CFrame = CFrame.new(availablespawnpoints[1].Position + Vector3.new(0,5,0))
				table.remove(availablespawnpoints, 1)
			end)
			
			if char then
				hrp.CFrame = CFrame.new(availablespawnpoints[1].Position + Vector3.new(0,5,0))
				table.remove(availablespawnpoints, 1)
			end
		end
	end
	
	intermission_re:FireAllClients("Starting", false)
	
	timer_re:FireAllClients(game_timer)
	
	task.wait(game_timer+2)
	
	print("Game Ended")
	
	deathfunction:Disconnect()

Here is the full code (If needed):

local players = game:GetService("Players")
local replicatedstorage = game:GetService("ReplicatedStorage")
local serverstorage = game:GetService("ServerStorage")

local meleefolder = serverstorage.Melee:GetChildren()
local rangedfolder = serverstorage.Ranged:GetChildren()
local specialfolder = serverstorage.Special:GetChildren()

local remotesfolder = replicatedstorage.Remotes
local intermission_re = remotesfolder.Intermission
local timer_re = remotesfolder.Timer

local mapsfolder = serverstorage.Maps:GetChildren()

--// settings

local min_plrs = 2

local game_timer = 120

--// game-functions

while task.wait() do
	
	-- check player-count
	
	repeat task.wait(1)
		warn("Not enough players!")
		intermission_re:FireAllClients("Not enough players!")
	until players.NumPlayers >= min_plrs
	
	print("Enough players to start the game!")
	
	-- enough player count to begin the game
	
	-- Intermission countdown
	
	intermission_re:FireAllClients("Intermission..", true)
	
	task.wait(1)
	
	for i = 10, 1, -1 do
		intermission_re:FireAllClients("Intermission: "..i)
		task.wait(1)
	end
	
	-- Picking random map
	
	local random_map = mapsfolder[math.random(1,#mapsfolder)]:Clone()
	random_map.Parent = workspace
	
	intermission_re:FireAllClients("Picking a random map..")
	
	task.wait(2.5)
	
	intermission_re:FireAllClients("Map Chosen: "..random_map.Name)
	
	task.wait(2.5)
	
	intermission_re:FireAllClients("Teleporting players..")
	
	task.wait(1)
	
	local spawnpoints = random_map:FindFirstChild("SpawnPoints")
	
	if spawnpoints then
		print("SpawnPoints found!")
	else
		warn("SpawnPoints not found!")
	end
	
	local availablespawnpoints = spawnpoints:GetChildren()
	
	local plrs = {}
	
	local deathfunction
	
	for _, player in pairs(players:GetPlayers()) do
		if player then
			local char = player.Character
			local hrp = char.HumanoidRootPart
			local hum = char.Humanoid
			
			for _, tool in pairs(player.Backpack:GetChildren()) do
				if tool.Name == "Beachball" and tool:IsA("Tool") then
					tool.Parent = workspace.Lobby
					tool.Ball.CFrame = workspace.Lobby.BeachballSpawn.CFrame
				end
			end

			for _, tool in pairs(char:GetChildren()) do
				if tool.Name == "Beachball" and tool:IsA("Tool") then
					tool.Parent = workspace.Lobby
					tool.Ball.CFrame = workspace.Lobby.BeachballSpawn.CFrame
				end
			end
			
			local random_melee = meleefolder[math.random(1,#meleefolder)]:Clone()
			local random_ranged = rangedfolder[math.random(1,#rangedfolder)]:Clone()
			local random_special = specialfolder[math.random(1,#specialfolder)]:Clone()
			
			random_melee.Parent = player.Backpack
			random_ranged.Parent = player.Backpack
			random_special.Parent = player.Backpack
			
			table.insert(plrs, player)
			
			deathfunction = player.CharacterAdded:Connect(function(char)
				local random_melee = meleefolder[math.random(1,#meleefolder)]:Clone()
				local random_ranged = rangedfolder[math.random(1,#rangedfolder)]:Clone()
				local random_special = specialfolder[math.random(1,#specialfolder)]:Clone()
				random_melee.Parent = player.Backpack
				random_ranged.Parent = player.Backpack
				random_special.Parent = player.Backpack

				repeat task.wait() until char:IsDescendantOf(workspace)
				char.HumanoidRootPart.CFrame = CFrame.new(availablespawnpoints[1].Position + Vector3.new(0,5,0))
				table.remove(availablespawnpoints, 1)
			end)
			
			if char then
				hrp.CFrame = CFrame.new(availablespawnpoints[1].Position + Vector3.new(0,5,0))
				table.remove(availablespawnpoints, 1)
			end
		end
	end
	
	intermission_re:FireAllClients("Starting", false)
	
	timer_re:FireAllClients(game_timer)
	
	task.wait(game_timer+2)
	
	print("Game Ended")
	
	deathfunction:Disconnect()
	
	random_map:Destroy()
	
	for i, player in pairs(plrs) do
		if player then
			local char = player.Character
			player:LoadCharacter()
		end
	end
end

I’ve looked for many posts, ideas or anything, but I can’t just seem to find my answer. Something that I did find however is Instance:DisconnectAllConnections(). This is some thing that I kind of want to achieve, but unfortunately it’s only for instance.

I deeply appreciate any kind of help!

2 Likes
local players = game:GetService("Players")
local replicatedstorage = game:GetService("ReplicatedStorage")
local serverstorage = game:GetService("ServerStorage")

local meleefolder = serverstorage.Melee:GetChildren()
local rangedfolder = serverstorage.Ranged:GetChildren()
local specialfolder = serverstorage.Special:GetChildren()

local remotesfolder = replicatedstorage.Remotes
local intermission_re = remotesfolder.Intermission
local timer_re = remotesfolder.Timer

local mapsfolder = serverstorage.Maps:GetChildren()

--// settings

local min_plrs = 2

local game_timer = 120

--// game-functions

while task.wait() do
	
	-- check player-count
	
	repeat task.wait(1)
		warn("Not enough players!")
		intermission_re:FireAllClients("Not enough players!")
	until players.NumPlayers >= min_plrs
	
	print("Enough players to start the game!")
	
	-- enough player count to begin the game
	
	-- Intermission countdown
	
	intermission_re:FireAllClients("Intermission..", true)
	
	task.wait(1)
	
	for i = 10, 1, -1 do
		intermission_re:FireAllClients("Intermission: "..i)
		task.wait(1)
	end
	
	-- Picking random map
	
	local random_map = mapsfolder[math.random(1,#mapsfolder)]:Clone()
	random_map.Parent = workspace
	
	intermission_re:FireAllClients("Picking a random map..")
	
	task.wait(2.5)
	
	intermission_re:FireAllClients("Map Chosen: "..random_map.Name)
	
	task.wait(2.5)
	
	intermission_re:FireAllClients("Teleporting players..")
	
	task.wait(1)
	
	local spawnpoints = random_map:FindFirstChild("SpawnPoints")
	
	if spawnpoints then
		print("SpawnPoints found!")
	else
		warn("SpawnPoints not found!")
	end
	
	local availablespawnpoints = spawnpoints:GetChildren()
	
	local plrs = {}
	
	local deathFunctions = {}
	
	for _, player in pairs(players:GetPlayers()) do
		if player then
			local char = player.Character
			local hrp = char.HumanoidRootPart
			local hum = char.Humanoid
			
			for _, tool in pairs(player.Backpack:GetChildren()) do
				if tool.Name == "Beachball" and tool:IsA("Tool") then
					tool.Parent = workspace.Lobby
					tool.Ball.CFrame = workspace.Lobby.BeachballSpawn.CFrame
				end
			end

			for _, tool in pairs(char:GetChildren()) do
				if tool.Name == "Beachball" and tool:IsA("Tool") then
					tool.Parent = workspace.Lobby
					tool.Ball.CFrame = workspace.Lobby.BeachballSpawn.CFrame
				end
			end
			
			local random_melee = meleefolder[math.random(1,#meleefolder)]:Clone()
			local random_ranged = rangedfolder[math.random(1,#rangedfolder)]:Clone()
			local random_special = specialfolder[math.random(1,#specialfolder)]:Clone()
			
			random_melee.Parent = player.Backpack
			random_ranged.Parent = player.Backpack
			random_special.Parent = player.Backpack
			
			table.insert(plrs, player)
			
			deathFunctions[player] = player.CharacterAdded:Connect(function(char)
				local random_melee = meleefolder[math.random(1,#meleefolder)]:Clone()
				local random_ranged = rangedfolder[math.random(1,#rangedfolder)]:Clone()
				local random_special = specialfolder[math.random(1,#specialfolder)]:Clone()
				random_melee.Parent = player.Backpack
				random_ranged.Parent = player.Backpack
				random_special.Parent = player.Backpack

				repeat task.wait() until char:IsDescendantOf(workspace)
				char.HumanoidRootPart.CFrame = CFrame.new(availablespawnpoints[1].Position + Vector3.new(0,5,0))
				table.remove(availablespawnpoints, 1)
			end)
			
			if char then
				hrp.CFrame = CFrame.new(availablespawnpoints[1].Position + Vector3.new(0,5,0))
				table.remove(availablespawnpoints, 1)
			end
		end
	end
	
	intermission_re:FireAllClients("Starting", false)
	
	timer_re:FireAllClients(game_timer)
	
	task.wait(game_timer+2)
	
	print("Game Ended")
	
    for player, deathFunction in deathFunctions do
	    deathfunction:Disconnect()
    end
	
	random_map:Destroy()
	
	for i, player in pairs(plrs) do
		if player then
			local char = player.Character
			player:LoadCharacter()
		end
	end
end

Edits:-

  • Added a deathFunctions array
  • Rather than dcing death function (which gets reset after every iteration), it instead loops the deathFunctions array which is in the format `{Player: RBXScriptConnection}

Credits to @theworldis_socruel for explanation lol

2 Likes

To elaborate on what you and @Mystxry12 did, you only had one variable, and you replaced it in the loop, causing that the connection only would exist for the last player iterated over in the loop. Jqck instead made an array(table) in order to hold onto all the connections and not overwrite the previous ones.

2 Likes

Could you explain what dcing is?

1 Like

Disconnecting char limit

1 Like

Ah ok, thanks. character limit

1 Like

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