Storing players in a table

I have this table

playersInRoundList = {}

and I store player names that are in the round as keys, so it will look like this mid-game

playersInRoundList = {["Soflowsen"] = true, ["Octopus"] = true}

Then when I need to get the player by name, I iterate through GetPlayers() and if the name of the player there is equal to the one in the table, then I use the player whose name that is.

I think it’s inefficient, but I don’t want to change it to using whole players as keys, because in most other languages (especially compiled), you would be unable to use objects (or arrays/tables) as indexes.

I was thinking of maybe making the players be values, so something like

playersInRoundList = {Soflowsen, Octopus}

but then I won’t be able to get players from list by key

playersInRoundList[Soflowsen]

How should I do that?

couldn’t you just do

if playersInRoundList[PlayerName] then
end

or am I misunderstanding on what you are trying to do

Right now, playersInRoundList contains names, not players, like this.

edit: I’m trying to find the best way to store players in a table, and I don’t want to store the player as a key, because most other languages can’t do that, and only support arrays, where the key can only be an index.

Not sure why you wouldn’t want to use player objects as keys, but if you insist then you should be using User Id’s to store the players.
Then, as @D0RYU said, you can do

if playersInRoundList[Player.UserId] then
end

just because roblox doesn’t use camelCase doesn’t mean you can’t use it

who cares

Lua has some built-in table functions that I’m pretty sure can solve your issue

-- modulescript
local Players = game:GetService("Players")

local RoundModule = {PlayersInRound = {}}

function RoundModule:Reset()
	table.clear(self.PlayersInRound)
end

function RoundModule:Start()
	RoundModule:Reset()
	for _, Player in pairs(Players:GetPlayers()) do
		table.insert(self.PlayersInRound, Player.Name)
	end
end

function RoundModule:CheckForPlayer(PlayerName : string): (boolean, number | any)
	local index = table.find(self.PlayersInRound, PlayerName)
	return index ~= nil, index
end

function RoundModule:RemovePlayer(PlayerName : string)
	local IsInRound, Index = RoundModule:CheckForPlayer(PlayerName)
	if IsInRound == true then
		table.remove(self.PlayersInRound,Index)
	end
end

return RoundModule

Would using objects as keys make it slower?

Are objects pointers? If so, then I think it wouldn’t

Maybe, but it probably won’t matter. Whatever task you’re doing is going to be much more expensive than indexing the player.

Using table.find versus indexing the table directly has a negligible if not no difference in work time. The only difference is that table.find is trying to find a key in the specified table and returns the index if it’s found.