Related to squid game, I am trying to achieve basically body numbers for the players. So when they join the game, they get set with a number that is in numerical order. 0-456.
If you have watched squid game you understand they get numbers when they first start.
So I want a player to spawn in with a set number.
How could I attempt this?
What solutions have I tried so far?
Mainly looked at functions on updating player counts and everything related to that.
I mainly just need the basic knowledge of giving a player a certain number when they join. I already know all the rest relating to positioning, and displaying the gui.
local PlayersService = game:GetService("Players")
local txt = script.Parent
local function UpdatePlayerCount()
txt.Text = "Players: ".. #PlayersService:GetPlayers() .."."
end
UpdatePlayerCount()
PlayersService.PlayerAdded:Connect(UpdatePlayerCount)
PlayersService.PlayerRemoving:Connect(UpdatePlayerCount)
Make a value and add to it when a player joins and subtract from it when player leaves, whenever value changes connect a changed event to do the update function based on the value.
-- serverscript
function set()
for i,v in pairs(game.Players:GetChildren()) do
if not v:GetAttribute("PlayerNumber") then -- if the player hasn't already been assigned a number
v:SetAttribute("PlayerNumber", i);
end
end
end
game.Players.PlayerAdded:Connect(function()
set();
end)
game.Players.PlayerRemoving:Connect(function()
set();
end)
You could then get a player’s number (from client or server) with player:GetAttribute("PlayerNumber");
Additionally, you could make it a little more advanced by caching the numbers of players who left and setting those as high priority:
-- serverscript
local cache = {};
game.Players.PlayerAdded:Connect(function(plr)
plr:SetAttribute("PlayerNumber", cache[1] or #game.Players:GetChildren()); -- set as number of player who left or as the last to join
table.remove(cache, 1); -- get rid of it once set
end)
game.Players.PlayerRemoving:Connect(function(plr)
if(plr:GetAttribute("PlayerNumber")) then
table.insert(cache, plr:GetAttribute("PlayerNumber"));
end
end)