Only one player gets removed from the game area after the match ends

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);

Is there any way to fix this? I have no idea why it only removes one player and stops instead of removing everyone.

try doing a

for index, name in ipairs(JoinedPlayers) do
print(index, name)
end

in the function and lmk the results, it could be that your list of players is bugged and only has 1 player in it to begin with

1 Like
local players = game:GetService("Players")

for _, player in ipairs(players:GetPlayers()) do
	player:LoadCharacter()
end

Simplest way to reload the characters of a server of players.

2 Likes

sorry for the late respsonse, thanks I’ll do that right now!

these are the results:
image
it loops through the table correctly but it only removes one person, do you think there’s something that’s stopping it from running twice