Optimization of Order-Agonistic Player Retrieval / Rotating Table

hey, you’re probably familiar with Minecraft.
there’s a mechanic in MC servers where if two players are close to an item, then the person who joined the server earlier is guaranteed the item.
i sought to devise my own solution to fix this issue for Roblox games. this issue is most prevalent in enemy detection systems or hitboxes (i.e. whoever joined first gets hit with priority in a region3 hitbox). my solution was a rotating table.

(lua5.4)

local currentPlayers = {1,2,3,4,5,6}
local playerIndex = 1

local function getPlayers()
    local newPlayers = {}
    local length = #currentPlayers
    for index, player in ipairs(currentPlayers) do
        newPlayers[(playerIndex + index - 1) % length + 1] = player
    end
    playerIndex = playerIndex % length + 1

    return newPlayers
end

print("normal:")
for _, v in ipairs(currentPlayers) do
    print(v)
end

for _ = 1, 8 do
    print("ok:")
    for _, v in ipairs(getPlayers()) do
        print(v)
    end
end

in an actual game, it would look closer to:
(luau)

local PlayerService = game:GetService('Players')

local currentPlayers = {}
local playerIndex = 1

local function playerAdded(player)
    table.insert(currentPlayers, player)
end

local function playerRemoving(player)
    table.remove(currentPlayers, table.find(currentPlayers, player))
    playerIndex = 1 -- no need to be more complicated
end

for _, player in ipairs(PlayerService:GetPlayers())
    playerAdded(player)
end
PlayerService.PlayerAdded:Connect(playerAdded)
PlayerService.PlayerRemoving:Connect(playerRemoving)

local function getPlayers()
    local newPlayers = {}
    local length = #currentPlayers
    for index, player in ipairs(currentPlayers) do
        newPlayers[(playerIndex + index - 1) % length + 1] = player
    end
    playerIndex = (playerIndex % length) + 1

    return newPlayers
end

print("normal:")
for _, v in ipairs(currentPlayers) do
    print(v)
end

for _ = 1, 8 do
    print("ok:")
    for _, v in ipairs(getPlayers()) do
        print(v)
    end
end

this working prototype took roughly 5 minutes to program- it’s a really easy implementation, and it’s O(n).

since Minecraft runs the item pickup check on ticks, there is no single tick where a given player is prioritized- or it is so chaotically difficult to predict (given multiple components of the game calling the getPlayers() function), that there is no single point in time where a player is prioritized.

however, my main concern is the optimization of it, if it is used in multiple parts of the game at high speeds.
the indexing of newPlayers in the for-loop + the for-loop is my primary concern:

(playerIndex + index - 1) % length + 1

followed by the modulo

playerIndex = (playerIndex % length) + 1

math is dirt cheap; however, if this functions run for tens of entities, for tens of players, many times a second, it starts to feel like a concern.

an easy fix (for a tick-based system like minecraft) would be to use the same getPlayers()-generated table for every system that calls the function in a given tick. if the item-pickup system calls getPlayers() in tick A, and an enemy-detection system calls getPlayers() in tick A as well, then the value of getPlayers() is shared between them.

a similar and easy fix for systems like many roblox games, those of which don’t operate on ticks, is to round the time (os.clock()) to the nearest tenth for both systems, and if they share the same rounded time value, they share the same getPlayers() output. this approach would require some caching.

however, both approaches, while functioning, clearly sacrifice game design in the process.

chatgpt generated this solution for me

local function getPlayers()
    local players = currentPlayers
    local length = #players
    local start = playerIndex

    local result = table.create(length)

    local out = 1

    for i = start, length do
        result[out] = players[i]
        out += 1
    end

    for i = 1, start - 1 do
        result[out] = players[i]
        out += 1
    end

    playerIndex = start == length and 1 or start + 1

    return result
end

but it includes two-loops, and i’m not sure if it’s as optimized as the solution could get.

review my code, and please show me if it can be further optimized. keep in mind, rotating tables are not necessary. i need per-call table uniqueness, the most mathematically-optimized working solution.

If im understanding this right, your issue is that indexing a table can be slow/high load when you are doing it a ton of times. If this is the case then maybe just have a single variable alongside your table that contains the value for the longest playing player and update it whenever someone leaves?

what does this accomplish?

|| || || || || ||

why do you need a copy of the table each time?
why not just:

-- module script
local PlayerService = game:GetService('Players')

local currentPlayers = {}

local function playerAdded(player)
	table.insert(currentPlayers, player)
end

local function playerRemoving(player)
	local index = table.find(currentPlayers, player)
	if not index then return end
	table.remove(currentPlayers, index)
end

for _, player in PlayerService:GetPlayers() do
	playerAdded(player)
end
PlayerService.PlayerAdded:Connect(playerAdded)
PlayerService.PlayerRemoving:Connect(playerRemoving)

return currentPlayers

?..

local function getPlayers()
    local newPlayers = {}
    local length = #currentPlayers
    for index, player in ipairs(currentPlayers) do
        newPlayers[(playerIndex + index - 1) % length + 1] = player
    end
    playerIndex = (playerIndex % length) + 1

    return newPlayers
end

because of this function. this is the heart of the post, and you removed it.

given an array of players {A, B, C, D, E}, this function returns:

-- n=1
{A, B, C, D, E}

-- n=2
{B, C, D, E, A}

-- n=3
{C, D, E, A, B}

-- ... cyclic

there’s no way around making a new table for my system.

Isn’t that what I just said? Keep an array that will keep track of which player has been in the game the longest. Use an array, not a dictionary. No loops needed.

Am I missing something?

The way I get it is you want something fast without loops. You want to know who has been playing the longest. An array with the players in order can do this (will always be first index). You could also have a variable linked with the table in some way so that the variable is the player object so you don’t need to reference the table each time.

If i’m wrong then you need to explain your issue a lot better.