How to assign a value to a player based on the players in the server

so i’m trying to make this game and i’m just starting and i can’t quite figure out how do to this…

so let me give you an example of what im trying to do

let’s play a player joins and there is already 3 players in the server they would be assigned player 4 cause they are the fourth person to join and so on

any help matters boys!

It depends what you mean honestly. For example, are you looking for that value to be how many people have joined that server total, or are you looking for assigning that value based on the amount of players currently in game.

Here is an example of a “how many people have joined the game”:

local Count = 0
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	Count = Count + 1
	local TheValue = Instance.new("IntValue")
	TheValue.Value = Count
	TheValue.Name = "CountValue"
	TheValue.Parent = plr
end)

Here is an example of “the current amount of players in the server”

Players.PlayerAdded:Connect(function(plr)
	local JoinPosition = #game.Players:GetChildren()
	local TheValue = Instance.new("IntValue")
	TheValue.Value = JoinPosition
	TheValue.Name = "JoinPosition"
end)

-- if you wanted the values to update whenever they leave, simply hook up a playersRemoving, detect the players who lefts JoinPosition value, then loop through all the players, if there value is bigger than the persons who left, then minus one from it. 

sorry man i already figured it out i’ll still give you the solution tho (i was overthinking it too much)

and anybody looking for the script:

game.Players.PlayerAdded:Connect(function(plr)
	local playerCount = #game.Players:GetPlayers()
	print(playerCount)
	local playervalue = plr:WaitForChild("Player")
	playervalue.Value = playerCount
end)

(player is the name of the value)

As the previous poster correctly indicated, you’ll need to account for players leaving.

local players = game:GetService("Players")

local function onPlayerAdded(player)
	local position = Instance.new("IntValue")
	position.Name = "Position"
	position.Value = #players:GetPlayers()
	position.Parent = player
end

local function onPlayerRemoving(player)
	local position = player.Position
	for _, _player in ipairs(players:GetPlayers()) do
		local _position = _player.Position
		if _position.Value > position.Value then
			_position.Value -= 1
		end
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
2 Likes

ok a bit of a road block… i need to do that but reverse. there is a value in the workspace named Player1 and the value is the username of whoever is player1 but the values should always exist the workspace like if there is no 6th player the string value will just be “error”

As in, Player1 should be the newest player and Player2 the second newest player etc…?

local players = game:GetService("Players")

local function onPlayerAdded(player)
	local position = Instance.new("IntValue")
	position.Name = "Position"
	position.Value = 1
	position.Parent = player
	
	for _, _player in ipairs(players:GetPlayers()) do
		if _player ~= player then
			local _position = _player.Position
			_position.Value += 1
		end
	end
end

local function onPlayerRemoving(player)
	for _, _player in ipairs(players:GetPlayers()) do
		if _player ~= player then
			local _position = _player.Position
			_position.Value -= 1
		end
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)

let me explain, so
image
the values should always exist in workspace player 1 being the oldest player 6 being the newest and if there are only lets say 4 players. value Player5 and value Player6 should just have error in the string

(the values are string values) also the string in the value should be the players username

did you get me? if you still in this post