Players in lobby

for a lobby system how would i get players to always be in the right position to something like this whenever a player joins:


and if a player leaves (for example p1) p2 would move to the first spot and everyone would move down one with one empty spot

basically fortnite lobby idk

3 Likes

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.

3 Likes

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”

1 Like

yh im already doin that i jus dont know how to update their positions in the lobby correctly

give me an example on how i would use an object value for this and use that to update pos

1 Like

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
2 Likes

this works except players leaving doesnt work

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)

1 Like

When player is being removed, their character is deleted therefore the Character variable is nil and the script will yield forever

thanks baby wwwwwwwwwwwwwwwwww

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.