Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I set the players names to seperate values? - SOLVED

I have a two-player game where I want to get their usernames as soon as they join in the game. I would do this by assigning each player's username to separate values (fx. "Value1" and "Value2"). Value1 would be the first player's username and Value2 for the second player's username. I can not wrap my brain around how to do this, I made this script print out the player's names individually. I just don't know how I would make them get in their own separate values.

for _, player in pairs(game.Players:GetPlayers()) do
	print(player.Name)
end

2 comments

1

You can't change player's in-game usernames.

0

I dont want to change the names of them, I want to get their names in two different values.

4 answers

Want to get the player's username? You can use tables.

local playersTable = {}

game.Players.PlayerAdded:Connect(function(player)
	table.insert(playersTable, player)
end)

for _, v in pairs(playersTable) do
	print(playersTable[_])
end

1 comment

0

hm doesnt seem to work for me

There is an event called PlayerAdded it is an event from game.Players another value that will be used is PlayerRemoving most things are explained in the script.

(the script will look more clean if you put it in roblox studio)

script:

local players = game.Players
local valueParent = game:GetService("ReplicatedStorage") -- change this to whatever you want the value's parent to be
local playerNumber = 0 -- number of players in the game

players.PlayerAdded:Connect(function(plr) -- when a player join the game
	if playerNumber == 0 then -- if there is zero players in the game
		playerNumber += 1 -- add one to the player counter if there is zero palyers
		local player1 = Instance.new("StringValue", valueParent)-- creating a value for the first player to join also sets the parent of the value to the "valueParent"
		player1.Name = "Player"..playerNumber -- the value's name is player*number of players*
		player1.Value = plr.Name -- the value stored inside is the new joiner's name
	elseif playerNumber == 1  then -- if there is already a player in the game
		playerNumber += 1 -- adding one to the player counter
		local player2 = Instance.new("StringValue", valueParent) -- creating a value for the second player to join
		player2.Name = "Player"..playerNumber -- the name will be player*number of player in game*
		player2.Value = plr.Name -- the value will be the new joiner's name
	end
end)

players.PlayerRemoving:Connect(function(plr)-- when a player leaves the game
	local children = valueParent:GetChildren()-- gets all the chidren of the player value's parents
	for i, child in pairs(children) do -- for every children of value Parent do
		if child:IsA("StringValue") then -- if the children is a string value then
			if child.Value == plr.Name then -- if the children's value is equle to the leaving player's name then
				child:Destroy() -- destroy their player value
				playerNumber -= 1 -- getting rid of one player counter
			end
		end
	end
end)

hope this helps :)

1 comment

0

This apparently only creates one value in ReplicatedStorage named 'Player1' and with the value put as 'Player2'

As TribotGamerGX mentioned, you cannot change a Player's name due to it being a read-only attribute. You can store their names within a Table, however.

You can achieve this by creating a callback function that inserts their username into a Table upon the invocation of the PlayerAdded RBXScriptSignal (Event) of the Players Service. With that, we can hold our program until there are two active Clients in the Server. When that is so, you can use unpack() to store both elements into the variables Value1 & Value2:

--###----------[[SERVICES]]----------###--
local Players = game:GetService("Players")



--###----------[[VARIABLES]]----------###--
local PlayerNames = {}



--###----------[[INITIALIZE]]----------###--
Players.PlayerAdded:Connect(function(Player)
	table.insert(PlayerNames, Player.Name)
end)



--###----------[[TEST]]----------###--
repeat wait() until #Players:GetPlayers() >= 2


local Value1, Value2 = unpack(PlayerNames)

print(Value1, Value2)

1 comment

0

This prints out "Player2, nil" for me

I just solved this, thanks for the ones who posted answers :)

This discussion is preserved as it was. New answers, comments, and votes are closed.