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