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)
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)
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)
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
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)
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:
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)