Player List Help

I simply dont know where to start when making this system
I am trying to make script that will be like when a player joins they will be given a value. Player1, 2, 3,or 4. then when they leave the value becomes blank so a new player can take the value. this will later on be the base of almost every other script in the game. Any ideas?

You’d start off by creating the values somewhere (I’m guessing a folder with the 4 stringvalues) You can do this in studio or through a script which would look like:

local playerValues = Instance.new("Folder")
playerValues.Parent = game.ReplicatedStorage
playerValues.Name = "Player Values"
-- copy and paste the below and change number to however many players you want to store a value for (if you want a big number of playervalues then i'd suggest making a table then a loop to create all the values to make the script smaller)
local player1 = Instance.new("StringValue") 
player1.Parent = playerValues
player1.Name = "Player 1"

After you’ve done this you’d need to create your joining/leaving functions that add/remove the players name to the value:

-- Player Joining
game.Players.PlayerAdded:Connect(function(plr)
    for i,v in ipairs(game.ReplicatedStorage["Player Values"]:GetChildren()) do -- runs a loop through player values folder
        if v.Value == "" then -- checks if player value is empty
            v.Value = plr.Name -- stores the player in the value (as a string)
            break -- breaks the loop once a player has been assigned a player valu
        end
    end
end)

-- Player Leaving
game.Players.PlayerRemoving:Connect(function(plr)
	for i,v in ipairs(game.ReplicatedStorage["Player Values"]:GetChildren()) do -- runs a loop through player values folder
		if v.Value == plr.Name then -- checks if player value is the player that is leaving
			v.Value = "" -- changes the player value to nothing
			break
		end
	end
end)
2 Likes

Thanks for all this Im gonna take a look at this and figure some things out! Where would I put these scripts?

All of this can go inside a script in ServerScriptService

1 Like

I’ve made this a bit easier for you and rather than you having to copy/paste new values, I’ve gone ahead and created the loop to create these values for you. Change the “numberOfPlayerValues” variable to however many players you want, I’ve tried to make this as noted and helpful as possible!
Your whole script should look like:

	local numberOfPlayerValues = 4 -- set this to however many number values you want
	-- creates player value folder
	local playerValues = Instance.new("Folder") 
	playerValues.Parent = game.ReplicatedStorage
	playerValues.Name = "Player Values"
	
	-- creates player values loop
	for i=1,numberOfPlayerValues do -- this loop will run however many times the the value of numberOfPlayerValues variables is set for
		local value = Instance.new("StringValue")
		value.Parent = playerValues
		value.Name = "Player "..i
	end

	game.Players.PlayerAdded:Connect(function(plr)
		for i,v in ipairs(game.ReplicatedStorage["Player Values"]:GetChildren()) do -- runs a loop through player values folder
			if v.Value == "" then -- checks if player value is empty
				v.Value = plr.Name -- stores the player in the value (as a string)
				break -- breaks the loop once a player has been assigned to a player value 
			end
		end
	end)

	game.Players.PlayerRemoving:Connect(function(plr)
		for i,v in ipairs(game.ReplicatedStorage["Player Values"]:GetChildren()) do -- runs a loop through player values folder
			if v.Value == plr.Name then -- checks if player value is the player that is leaving
				v.Value = "" -- changes the player value to nothing
				break -- breaks the loop
			end
		end
	end)
1 Like

You are amazing and this is very helpful! Do you know where I can add prints into there?

Oh wait I just remembered I can check the string values. Nvm sorry

This is an overcomplicated way of achieving the same from a solely server-sided script.

local players = game:GetService("Players")
local playerList = {} --list of players array

players.PlayerAdded:Connect(function(player)
	table.insert(playerList, player)
	for i, v in pairs(playerList) do
		if v.Name == player.Name then --player added matches player from list
			local playerNum = Instance.new("IntValue") --create intvalue instance for player
			playerNum.Name = "PlayerNumber"
			playerNum.Value = i
			playerNum.Parent = player
		end
	end
end)

players.PlayerRemoving:Connect(function(player)
	for i, v in pairs(playerList) do
		if v.Name == player.Name then --player leaving matches player from list
			table.remove(playerList, i)
		end
	end
	for i, v in pairs(playerList) do
		v:FindFirstChild("PlayerNumber"):Destroy() --destroy all intvalue instances of all players
		local playerNum = Instance.new("IntValue") --reassign new intvalue instances with corresponding numbers
		playerNum.Name = "PlayerNumber"
		playerNum.Value = i
		playerNum.Parent = player
	end
end)
1 Like