local alive = {} --table that marks who are alive and who are not local player_data = {} --table that stores data that will be deleted in the end of the round for i,player in pairs(game.Players:GetPlayers()) do if player.Character then local character = player.Character local humanoid = character:FindFirstChild("Humanoid") if humanoid and humanoid.Health > 0 then table.insert(alive,player) local died_connection local function died() table.remove(alive,i) if died_connection then died_connection:disconnect() end end local died_connection = humanoid.Died:connect(died) --connection one player_data[player].died_connection = died_connection end end end local holder = alive --for balancing teams local alpha,beta = {},{} --all team members, even the dead, so that they will be rewarded if team wins local alpha_alive,beta_alive = {},{} --team members alive --i left out the sorting system here for team in tuple(alpha_alive,beta_alive) do for i,p in pairs(team) do if p.Character then local c = p.Character if c then local hum = c:FindFirstChild("Humanoid") if hum then local team_value = Instance.new("StringValue") team_value.Name = "TEAM" team_value.Value = team == alpha_alive and "ALPHA" or team == beta_alive and "BETA" local died_connection local function died() table.remove(team,i) if died_connection then died_connection:disconnect() end end local died_connection = hum.Died:connect(died) --connection two player_data.team_died_connection = died_connection end end end end
What I want is to make connection one and connection two one connection. But if I'd try to move the connection one lower so that I can have a longer died function that removes the player from both alive and team_alive tables, it might leave the player at spawn when the round starts if he/she resets. That is a huge problem, and I don't want to use 2 separate connections. I of course couldn't move connection two towards the first one, because the sorting hasn't happened there. I also don't want to sort players that won't join the round to teams. And sorting can't happen inside a for loop. There are rather more of fail attempts than solutions to this problem.