Players not getting removed from a table after leaving

I’m working on a gameshow game and I want the players to be removed from the table after they leave buts it’s not working.

players.PlayerRemoving:Connect(function(player)
	local player_name = player.Name
	for i = 1, #game_players do
		if game_players[i] == player_name then
			table.remove(game_players, i)
		end
	end
	print(game_players)
end)
3 Likes

You are atrempting to remove an index, if the table key is set to player name it wont work.

I would just do game_players[player.Name] = nil and set the value like game_players[player.Name] = "whatever value here" when they first join.

2 Likes

One optimisation you can make, just break at the end when you have found the correct player.

for i = 1, #game_players do
		if game_players[i] == player_name then
			table.remove(game_players, i)
                        break
		end
	end

save the roblox engine as much as we can
your identifier is not set properly, maybe your loop isnt finding the player
how are you saving the player to the table?

I suggest using a dictionary and when the player is in the game, their name is set to “true” and when they lave you can just do dictionary[player.Name] = nil to remove them rather then needing to set up any loops

1 Like

cant you just do this?

game.Players.PlayerRemoving:Connect(function(player)
 table.remove(game_players, table.find(player.Name))
end)
1 Like

Hi, you should do this instead:

Players.PlayerRemoving:Connect(function(Player)
    game_players[Player.Name] = nil
end)

Removing the index directly when a player removes makes the code a lot cleaner and saves your server resources (especially so considering larger servers would generally mean you’d have to iterate through more to find the player’s name).

The issue is though is that its set an as array

In that case, you can do

Players.PlayerRemoving:Connect(function(Player)
   local found = table.find(game_players,Player.Name); 
   if found then 
      table.remove(game_players,found)
   end
end

table.find returns an index for a value (if that value exists in the table), and table.remove allows you to remove a specific index from the table.

game.Players.PlayersAdded:Connect(function(player)
table.insert(game_players, player.Name)
end)

ive tried this, the real issue is that array data is set up within luau to use numbers to identify a string. so i cant try and do

local player = game.Players.Metroid
local found = table.find(game_players, player.Name)

if found then table.remove(game_players, found) end

ima gonna attempt converting the array to a dictionary

This is fine? Arrays are characterized by values ordered by ascending numerical indices. table.find matches the value and returns the numerical index at which that value was found. This number is passed to table.remove so it may remove the value at that index from the array. There is no conflict with the player’s name being a string.

This sounds like an issue with your event listener being connected to Players.PlayerRemoving

1 Like

I’ll check this out, may be an issue with the table since the PlayerRemove() local function I’ve been using as a similar code structure but only recently been bugging.

Send your full script, that way we can get a look at the bigger picture

if you’re doing this on studio you also need to bind game:BindToClose() otherwise the server shuts down before playeremoving can fire

1 Like