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!