By utilizing table.insert and table.remove for the action of joining the lobby and leaving the lobby. You can have a table/array representing the players’ positions. Then make a function to update the players’ positions every time the table/array is changed.
We cant give full scripts. However, you can maybe use this as an example:
Make a button, then insert a local script into the button. After that, insert a server script into ServerScriptService.
Finally, add a remote event in replicated storage.
After youve done that, you can detect if the player has clicked said button, then fire the remote event.
In the server script, you can use a table (as said by @Tracccck ) and use table.insert() on the players character.
Then, in the server script, you can add a loop that will sort the characters by position using the table I had mentioned above.
With enough checking, maybe with an object value to see if someones already in that spot, you can make something like “the fortnite lobby selection”
Use the index of the table/array of the players in lobby
Then use them to set the players’ CFrames/Positions
Something like this
local Prepositions = {
workspace.LobbyPart1,
workspace.LobbyPart2,
...
}
local PlayersInLobby = {}
local function AddPlayerToLobby(plr)
table.insert(PlayersInLobby, plr)
end
local function RemovePlayerFromLobby(plr)
local ind = table.find(PlayersInLobby, plr)
if ind ~= nil then
table.remove(PlayersInLobby, ind)
end
end
local function UpdatePositionOfPlayers(PlayersArray)
for i, player in pairs(PlayersArray) do
player.Character.HumanoidRootPart.CFrame = Prepositions[i].CFrame
end
end
local Prepositions = {
LobbySpawns:WaitForChild("Player1"),
LobbySpawns:WaitForChild("Player2"),
LobbySpawns:WaitForChild("Player3"),
LobbySpawns:WaitForChild("Player4"),
LobbySpawns:WaitForChild("Player5"),
LobbySpawns:WaitForChild("Player6"),
LobbySpawns:WaitForChild("Player7"),
LobbySpawns:WaitForChild("Player8"),
LobbySpawns:WaitForChild("Player9"),
...
}
local PlayersInLobby = {}
local function AddPlayerToLobby(char)
table.insert(PlayersInLobby, char)
char.Parent = Lobby:WaitForChild("Players")
local chosenAnim = lobbyanims[math.random(1, #lobbyanims)]
repeat task.wait() until chosenAnim ~= nil
char.Humanoid.Animator:LoadAnimation(chosenAnim):Play()
end
local function RemovePlayerFromLobby(char)
local ind = table.find(PlayersInLobby, char)
if ind ~= nil then
table.remove(PlayersInLobby, ind)
end
end
local function UpdatePositionOfPlayers(PlayersArray) --player is actually the character
for i, char in pairs(PlayersArray) do
local spawnPart = Prepositions[i]
char.HumanoidRootPart.CFrame = spawnPart.CFrame
print(spawnPart)
end
end
game:GetService("Players").PlayerAdded:Connect(function(Player)
if not Player:HasAppearanceLoaded() then Player.CharacterAppearanceLoaded:Wait() end
--game:GetService("ContentProvider"):PreloadAsync({Player.Character})
local Character = Player.Character
Character.Archivable = true
local CharacterClone = Character:Clone()
CharacterClone:FindFirstChild("Animate"):Destroy()
AddPlayerToLobby(CharacterClone)
UpdatePositionOfPlayers(PlayersInLobby)
end)
game:GetService("Players").PlayerRemoving:Connect(function(Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
RemovePlayerFromLobby(Character)
UpdatePositionOfPlayers(PlayersInLobby)
end)