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.