How can I insert a player name Into a table as a string?

This might be a dumb question but I am still new to table values etc. How can I insert a player name Into a table value as a string? So In short insert in a table a string value, that I can acess without using numbers.

the script:

local GamePlayers = {}

function PlayerAdded(NewPlr)
	table.insert(GamePlayers,NewPlr.Name)
	print(GamePlayers)
end
function PlayerLeft(NewPlr)
	table.remove(GamePlayers,NewPlr.Name)
	print(GamePlayers)
end


game.Players.PlayerAdded:connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(PlayerLeft)

What I want:

local GamePlayers = {

'63il',

'Player1',

'Player2'

}

What I get:

local GamePlayers = {
[1] = "63il",
[2] = "Player1",
[3] = "Player2"
}

any help is appreciated!

GamePlayers[playerName] = whateverYouWantJustNotNil

What you could do here is:
table.remove(GamePlayers, table.find(GamePlayers, NewPlr.Name))

Or alternatively do what @azqjanna said and do something like:
GamePlayers[NewPlr.Name] = true

You can remove things from maps the same way:
GamePlayers[playerName] = nil

This also does not care if playerName was never actually in the table, not that it should be possible in this application.

1 Like

Yeah, I probably should have mentioned that. My first response with the table.remove is solely for the array approach OP was currently using

So when the player leaves, I can just set the value to nil ?

Yes. If an entry in the table for a given key has the value nil, it is considered to not be in the table.

Wow thanks! You saved me alot of time there!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.