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