Why does ServerName print out nil?

Hello!!

I have made a script where it creates a new string value instance where its value is the player’s name and … " 's Server" I have also made it so, it invokes a remote function.

    local ServerName = Instance.new("StringValue")
	ServerName.Parent = ServerScriptService.ServerIDHandler.ServerNames
	ServerName.Name = player.Name .. " 's SERVER"
	ServerName.Value = player.Name .. " 's SERVER"
	
	game.ReplicatedStorage.ServerNames:InvokeClient(player, ServerName)

In my local script, I say when it recieves that remote event it prints out the ServerName. However it prints out nil.

local player = game:GetService("Players")

local ServerGui = script.Parent
local ServerName = ServerGui.Server.ServerName

game.ReplicatedStorage.ServerNames.OnClientInvoke = function(player, ServerName)
	print(ServerName)
	
	
	
end

2 Likes

Isn’t ServerName an Instance itself? If you want to print out the value of ServerName, just add:


	print(ServerName.Value)

1 Like

Got this error when printing(ServerName.Value)

10:16:31.722 Players.planeboy2021.PlayerGui.ServerGUI.ServerNameScript:7: attempt to index nil with ‘Value’ - Client - ServerNameScript:7

1 Like

Anything that is in ServerScriptService or ServerStorage isn’t replicated to clients.

1 Like

Oh! Then how would I get the ServerName?

1 Like

You could just send the value (ServerName.Value instead of ServerName) instead.

1 Like

What do you mean by sending the value?

1 Like

Like this?

    local ServerName = Instance.new("StringValue")
	ServerName.Parent = ServerScriptService.ServerIDHandler.ServerNames
	ServerName.Name = player.Name .. " 's SERVER"
	ServerName.Value = player.Name .. " 's SERVER"
	
	game.ReplicatedStorage.ServerNames:InvokeClient(player, ServerName.Value)
1 Like

Yeah​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

1 Like

Oh wait

You don’t need to include the player argument. The purpose of passing the player argument in the server was so that the game knew where to fire the RemoteFunction to. Simply emit the player argument here and you should be alright

It still prints out nil.

image_2024-12-30_102200060

local player = game:GetService("Players")

local ServerGui = script.Parent
local ServerName = ServerGui.Server.ServerName

game.ReplicatedStorage.ServerNames.OnClientInvoke = function(PlayerServerName)
	print(PlayerServerName.Value)
	
	
	
end
local ServerName = Instance.new("StringValue")
	ServerName.Name = player.Name .. " 's SERVER"
	ServerName.Value = player.Name .. " 's SERVER"
	ServerName.Parent = ServerScriptService.ServerIDHandler.ServerNames
    -- set the parent as the last property because it's more optimized
	
	game:GetService("ReplicatedStorage").ServerNames:InvokeClient(player, ServerName.Value)
local player = game:GetService("Players")

local ServerGui = script.Parent
local ServerName = ServerGui.Server.ServerName

game:GetService("ReplicatedStorage").ServerNames.OnClientInvoke = function(PlayerServerName)
	print(PlayerServerName)
end
1 Like

Thank now it prints out the proper ServerName!

1 Like

Sorry, but when I try to join the server, it prints out nil.
@cakeycocoa

Not sure what the problem could be, I loaded up a test place and copied the same scripts in and it worked for me.
image

1 Like

So When a player creates a server, it prints out the ServerName, When a player joins the server it prints out nil.

ServerScript:

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

local ServerIDHandler = ServerScriptService:FindFirstChild("ServerIDHandler") or Instance.new("Folder", ServerScriptService)
ServerIDHandler.Name = "ServerIDHandler"

local ListofIds = ServerIDHandler:FindFirstChild("ListofIds") or Instance.new("Folder", ServerIDHandler)
ListofIds.Name = "ListofIds"

ReplicatedStorage.CreateServer.OnServerInvoke = function(player)
	local ServerID = tostring(math.random(10000, 99999))
	local ServerEntry = Instance.new("StringValue")
	ServerEntry.Value = ServerID
	ServerEntry.Name = player.Name .. " 's Server"
	ServerEntry.Parent = ListofIds

	local ServerName = Instance.new("StringValue")
	ServerName.Name = player.Name.. "'s SERVER"
	ServerName.Value = player.Name.. "'s SERVER"
	ServerName.Parent = ServerScriptService.ServerIDHandler.ServerNames
	-- set the parent as the last property because it's more optimized

	game:GetService("ReplicatedStorage").ServerNames:InvokeClient(player, ServerName.Value)

	player.AncestryChanged:Connect(function()
		if not player.Parent then
			local playerServerID = ListofIds:FindFirstChild(player.Name .. " 's Server")
			if playerServerID then
				playerServerID:Destroy()
			end
		end
	end)

	return ServerID
end

ReplicatedStorage.JoinServer.OnServerInvoke = function(player, ServerID)
	for _, server in ipairs(ListofIds:GetChildren()) do
		if server.Value == ServerID then
			print(player.Name .. " successfully joined server with ID:", ServerID)
			game.ReplicatedStorage.ServerNames:InvokeClient(player)
			
			
			
			return true
		end
	end
	warn(player.Name .. " failed to join server with ID:", ServerID)
	return false
end

Wait is this the issue?

It was the issue, I just removed the line and now it works!

ReplicatedStorage.JoinServer.OnServerInvoke = function(player, ServerID)
	for _, server in ipairs(ListofIds:GetChildren()) do
		if server.Value == ServerID then
			print(player.Name .. " successfully joined server with ID:", ServerID)
			game.ReplicatedStorage.ServerNames:InvokeClient(player) -- This Line
			
			
			
			return true
		end
	end
	warn(player.Name .. " failed to join server with ID:", ServerID)
	return false
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.