I’m trying to make a round system that adds the player’s username to a table once they join the round and remove them after they leave or get killed. When the match ends, it teleports everyone back to spawn.
The issue is instead of teleporting everyone in the table back to spawn, it only teleports one person.
I don’t know what the problem is and I’ve tried to debug it, but there’s no errors in the output whatsoever and I’m pretty sure the remove function adds and removes the players from the table correctly.
Any help is appreciated, thank you so much!! (if you need more information or anything else please ask)
This is the script I’m using (the function not working is the RemoveAllFromGame one)
---[[Services]]---
local players = game:GetService("Players");
local replicatedStorage = game:GetService("ReplicatedStorage");
---[[Variables]]---
local events = replicatedStorage:WaitForChild("Events");
local killEvent = events:WaitForChild("Kill");
local joinEvent = events:WaitForChild("JoinGame");
local leaveEvent = events:WaitForChild("LeaveGame");
local data = game.ReplicatedStorage:WaitForChild("Data"):WaitForChild("Round");
local inRound = data:WaitForChild("InRound");
local roundLength = data:WaitForChild("RoundLength");
local transition = require(replicatedStorage:WaitForChild("Modules"):WaitForChild("Transition"))
local onGameEnd = game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("Round"):WaitForChild("End");
local JoinedPlayers = {};
local function GetPlayerFromName(name)
for _, player in pairs(players:GetPlayers()) do
if player.Name:lower() == name:lower() then
return player;
end;
end;
end
local function add(player)
table.insert(JoinedPlayers, player.Name);
print("Player ".. player.Name .." has been added.")
end; joinEvent.Event:Connect(add);
local function remove(player)
local position = table.find(JoinedPlayers, player.Name);
if position then
table.remove(JoinedPlayers, position);
end;
print("Player ".. player.Name .." has been removed.")
end; leaveEvent.Event:Connect(remove); players.PlayerRemoving:Connect(remove);
local function removeFromGame(player)
local character = player.Character
if player and character then
remove(player)
local spawns = workspace:WaitForChild("Lobby"):WaitForChild("Spawns"):GetChildren()
local random = spawns[math.random(1, #spawns)]
if random then
transition.new(player); wait(0.5)
local hrp = character:WaitForChild("HumanoidRootPart"); hrp.Position = (random.Position + Vector3.new(0, 1, 0))
end
end
print("Player ".. player.Name .." has been removed from the game.")
end;
local function removeAllFromGame()
for index, name in ipairs(JoinedPlayers) do
local player = (GetPlayerFromName(name));
local character = player.Character;
if player then
wait(roundLength.Value / 3);
removeFromGame(player)
end;
end;
end; onGameEnd.Event:Connect(removeAllFromGame)
local function onKill(targetchar, player)
removeFromGame(targetchar)
local kills = 0
-- this is not finished.
end; killEvent.Event:Connect(onKill);