Player.Name prints out nil

Hello!

I have made two scripts, a server and local script.

ServerScript = makes a number value and stores it inside a folder which is in the script. Also, fires a remote Event through a for loop, with the parameters, ReceivePlayer, player.Name.

Local script = makes a textlabel and puts the text as player.name.

Issues = the player.name in the textlabel is nil.

Any solutions?

ServerScript

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local MaxPlayersFolder = ServerScriptService.MaxPlayers.MaxPlayersFolder

ReplicatedStorage.MaxPlayers.OnServerInvoke = function(player)
	
	local MaxPlayersValue = Instance.new("NumberValue") -- This makes a number value which tracks how many people are in a server/lobby
	MaxPlayersValue.Parent = MaxPlayersFolder
	MaxPlayersValue.Value = 1
	MaxPlayersValue.Name = player.Name .. " 's Server"
	
	for _, ReceivePlayer in ipairs(game.Players:GetPlayers()) do
		ReplicatedStorage.JoinServers.JoinServerPlayer:FireClient(ReceivePlayer, player.Name) -- here is the for loop
	end	
	
	if MaxPlayersValue then
		return true
	else
		return false
	end


end

ReplicatedStorage.JoinServers.JoinServerServer.OnServerEvent:Connect(function(player)
	local MaxPlayerValue = MaxPlayersFolder:FindFirstChild(player.Name .. " 's Server")
	if MaxPlayerValue then
		MaxPlayerValue.Value = MaxPlayerValue.Value + 1
		
		if MaxPlayerValue.Value >5 then
			print("more than five players")
		end
	end
end)

ReplicatedStorage.CloseFunction.CloseServerUI.OnServerEvent:Connect(function(player)
	local MaxPlayerValue = MaxPlayersFolder:FindFirstChild(player.Name .. " 's Server")
	if MaxPlayerValue then
		MaxPlayerValue.Value = MaxPlayerValue.Value - 1
	end
end)

Local Script:

local Players = game:GetService("Players")
local PlayerNameText = script.Parent.PlayerNameText

game.ReplicatedStorage.CreateServers.CreateServerPlayer.OnClientEvent:Connect(function()

	print("recieved")
	PlayerNameText.Text = Players.LocalPlayer.Name
	PlayerNameText:Destroy() -- Destroys text label bc somehow it makes two
end)

game.ReplicatedStorage.JoinServers.JoinServerPlayer.OnClientEvent:Connect(function(player) 
	print(tostring(player)) -- it prints player.name
	local playerTextClone = PlayerNameText:Clone()
	playerTextClone.Text = tostring(player) -- this line is nil
	playerTextClone.Parent = script.Parent
end)

game.ReplicatedStorage.CloseFunction.LeaveServer.OnClientEvent:Connect(function(player)
	local PlayerTextClone = script.Parent:FindFirstChild(Players.LocalPlayer.Name)
end)
8 Likes

Ok so I think I know the problem.

This destroys the text. Thus it never gets displayed or whatever. To fix this, remove the destroy.

The reason there are two of these things appearing is cause you create another one here:

Thus creating two.

4 Likes

I have fixed the two appearing, Here is a picture of what it looks like

The first line (my username) is the server creator

The second line is nil, this is when the player joins the server

4 Likes

On the server script you are firing the client passing ReceivePlayer and player.Name.

But on the client you are accessing the player instance wich is the first param

Try this:

game.ReplicatedStorage.JoinServers.JoinServerPlayer.OnClientEvent:Connect(function(player) 
	print(tostring(player)) -- it prints player.name
	local playerTextClone = PlayerNameText:Clone()
	playerTextClone.Text = player.Name -- this line is nil
	playerTextClone.Parent = script.Parent
end)
4 Likes

I got this error

10:15:24.556 Players.planeboy2021.PlayerGui.ServerGUI.Server.Players.PlayersScript:12: attempt to index nil with ‘Name’ - Client - PlayersScript:12

2 Likes

Try specifing both params like this:

game.ReplicatedStorage.JoinServers.JoinServerPlayer.OnClientEvent:Connect(function(player,plrName) 
	print(tostring(player)) -- it prints player.name
	local playerTextClone = PlayerNameText:Clone()
	playerTextClone.Text = player.Name -- this line is nil
	playerTextClone.Parent = script.Parent
end)
3 Likes

Nope, the same error still appears.

2 Likes

Oh yeah i forgot that the first param is the player you are calling

2 Likes

Here you are passing your name, you fixed this already if i understood ?
If yes do you get nil also on the first print on the client side where you have the comment saying “-- it prints player.name”

for _, ReceivePlayer in ipairs(game.Players:GetPlayers()) do
		ReplicatedStorage.JoinServers.JoinServerPlayer:FireClient(ReceivePlayer, player.Name) -- here is the for loop
	end	
2 Likes

When i print it on the client it did print out the player.name not i get that error on the same line.

1 Like

So you are passing ReceivePlayer instead of player.Name ?

Can you repost the code meaby it could help

2 Likes

ServerScript

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local MaxPlayersFolder = ServerScriptService.MaxPlayers.MaxPlayersFolder

ReplicatedStorage.MaxPlayers.OnServerInvoke = function(player)
	
	local MaxPlayersValue = Instance.new("NumberValue")
	MaxPlayersValue.Parent = MaxPlayersFolder
	MaxPlayersValue.Value = 1
	MaxPlayersValue.Name = player.Name .. " 's Server"
	
	for _, ReceivePlayer in ipairs(game.Players:GetPlayers()) do
		ReplicatedStorage.JoinServers.JoinServerPlayer:FireClient(ReceivePlayer, player)
	end	
	
	if MaxPlayersValue then
		return true
	else
		return false
	end


end

ReplicatedStorage.JoinServers.JoinServerServer.OnServerEvent:Connect(function(player)
	local MaxPlayerValue = MaxPlayersFolder:FindFirstChild(player.Name .. " 's Server")
	if MaxPlayerValue then
		MaxPlayerValue.Value = MaxPlayerValue.Value + 1
		
		if MaxPlayerValue.Value >5 then
			print("more than five players")
		end
	end
end)

ReplicatedStorage.CloseFunction.CloseServerUI.OnServerEvent:Connect(function(player)
	local MaxPlayerValue = MaxPlayersFolder:FindFirstChild(player.Name .. " 's Server")
	if MaxPlayerValue then
		MaxPlayerValue.Value = MaxPlayerValue.Value - 1
	end
end)

Local script:

local Players = game:GetService("Players")
local PlayerNameText = script.Parent.PlayerNameText

game.ReplicatedStorage.CreateServers.CreateServerPlayer.OnClientEvent:Connect(function()

	print("recieved")
	PlayerNameText.Text = Players.LocalPlayer.Name
	PlayerNameText:Destroy()
end)

game.ReplicatedStorage.JoinServers.JoinServerPlayer.OnClientEvent:Connect(function(player, plrname) 
	print(tostring(player.Name)) 
	local playerTextClone = PlayerNameText:Clone()
	playerTextClone.Text = player.Name 
	playerTextClone.Parent = script.Parent
end)

game.ReplicatedStorage.CloseFunction.LeaveServer.OnClientEvent:Connect(function(player)
	local PlayerTextClone = script.Parent:FindFirstChild(Players.LocalPlayer.Name)
end)

2 Likes
game.ReplicatedStorage.JoinServers.JoinServerPlayer.OnClientEvent:Connect(function(playerName) 
	local playerTextClone = PlayerNameText:Clone()
	playerTextClone.Text = playerName 
	playerTextClone.Parent = script.Parent
end)
3 Likes

for the fourth line i did tostring(PlayerName) but the second line of the picture is still nil

3 Likes

So you want to pass the player name to every other palyer ?

for _, ReceivePlayer in ipairs(game.Players:GetPlayers()) do
		ReplicatedStorage.JoinServers.JoinServerPlayer:FireClient(ReceivePlayer, player.Name)
	end	

Client:

game.ReplicatedStorage.JoinServers.JoinServerPlayer.OnClientEvent:Connect(function(plrname) 
	print(tostring(plrname)) 
	local playerTextClone = PlayerNameText:Clone()
	playerTextClone.Text = plrname
	playerTextClone.Parent = script.Parent
end)

Let me know if the first print works

4 Likes

Got this error
10:52:51.101 Unable to assign property Text. string expected, got nil - Client - PlayersScript:14

on this script (local script line 14)

game.ReplicatedStorage.JoinServers.JoinServerPlayer.OnClientEvent:Connect(function(plrname) 
	print(tostring(plrname)) 
	local playerTextClone = PlayerNameText:Clone()
	playerTextClone.Text = plrname -- this line
	playerTextClone.Parent = script.Parent
end)
4 Likes

Nope the print statement prints out nil.

2 Likes

How do you even make it fire so that it can send through the client? Also I think it only gives nil if the player doesn’t know it exist in the server (The server doesn’t know the name of the player) or the client itself… But I know it should also work on the client if it exist on the server. I just don’t know how do you make the function work so that I can fix it like for example:

game.ReplicatedStorage.CreateServers.CreateServerPlayer.OnClientEvent:Connect(function()

	print("recieved")
	PlayerNameText.Text = Players.LocalPlayer.Name
	PlayerNameText:Destroy()
end)

Like do you use the textbutton to fireallclient or fireclient inside the serverscript?

My possible solution to this is to add an if statement if it actually exist:

ServerScript:

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local MaxPlayersFolder = ServerScriptService.MaxPlayers.MaxPlayersFolder

ReplicatedStorage.MaxPlayers.OnServerInvoke = function(player)

	local MaxPlayersValue = Instance.new("NumberValue") -- This makes a number value which tracks how many people are in a server/lobby
	MaxPlayersValue.Parent = MaxPlayersFolder
	MaxPlayersValue.Value = 1
	MaxPlayersValue.Name = player.Name .. " 's Server"

	for _, ReceivePlayer in ipairs(game.Players:GetPlayers()) do
		if player ~= nil then -- Added an if statement here
		ReplicatedStorage.JoinServers.JoinServerPlayer:FireClient(ReceivePlayer, player) -- here is the for loop
		end
	end	

	if MaxPlayersValue then
		return true
	else
		return false
	end


end

ReplicatedStorage.JoinServers.JoinServerServer.OnServerEvent:Connect(function(player)
	local MaxPlayerValue = MaxPlayersFolder:FindFirstChild(player.Name .. " 's Server")
	if MaxPlayerValue then
		MaxPlayerValue.Value = MaxPlayerValue.Value + 1

		if MaxPlayerValue.Value >5 then
			print("more than five players")
		end
	end
end)

ReplicatedStorage.CloseFunction.CloseServerUI.OnServerEvent:Connect(function(player)
	local MaxPlayerValue = MaxPlayersFolder:FindFirstChild(player.Name .. " 's Server")
	if MaxPlayerValue then
		MaxPlayerValue.Value = MaxPlayerValue.Value - 1
	end
end)

LocalScript or ClientScript:

local Players = game:GetService("Players")
local PlayerNameText = script.Parent.PlayerNameText

game.ReplicatedStorage.CreateServers.CreateServerPlayer.OnClientEvent:Connect(function()

	print("recieved")
	PlayerNameText.Text = Players.LocalPlayer.Name
	PlayerNameText:Destroy()
end)

game.ReplicatedStorage.JoinServers.JoinServerPlayer.OnClientEvent:Connect(function(player) 
	print(tostring(player.Name)) -- Prints player.Name

	if player.Name ~= nil then -- try to remove this if statement if its returning player.Name because we have an if statement already inside the serverscript
	local playerTextClone = PlayerNameText:Clone()
	playerTextClone.Text = player.Name 
	playerTextClone.Parent = script.Parent
	end
end)

game.ReplicatedStorage.CloseFunction.LeaveServer.OnClientEvent:Connect(function(player)
	local PlayerTextClone = script.Parent:FindFirstChild(Players.LocalPlayer.Name)
end)

Let me know if it works !

4 Likes