Players.Player1.PlayerGui.ServerGUI.Server.Players.PlayersScript:42: invalid argument #1 to ‘ipairs’

Hello!

I have encountered this error and I have no idea what is causing the problem.

18:47:01.932 Players.Player1.PlayerGui.ServerGUI.Server.Players.PlayersScript:42: invalid argument #1 to ‘ipairs’ (table expected, got nil) - Client - PlayersScript:42

If you are wondering what this is, it is a ui server whch many games use. Players can create a server and join that server.

Local script:

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

-- UI and Debounce
local PlayerNameText = script.Parent.PlayerNameText
local debounce = false

-- Server Variables
local myServerName
local InServer = false
local playerTable = {}

-- Player Joining
local function HandlePlayerJoin(plrName)
	if not script.Parent:FindFirstChild(plrName) and not table.find(playerTable, plrName) then
		local playerTextClone = PlayerNameText:Clone()
		playerTextClone.Text = plrName
		playerTextClone.Name = plrName
		playerTextClone.Parent = script.Parent
		table.insert(playerTable, plrName)
	end
end

-- Player Leaving
local function HandlePlayerLeave(plrName)
	if script.Parent:FindFirstChild(plrName) and table.find(playerTable, plrName) then
		local playerTextClone = script.Parent:FindFirstChild(plrName)
		playerTextClone:Destroy()
		table.remove(playerTable, table.find(playerTable, plrName))
	end
end

-- Join Server Event
ReplicatedStorage.JoinServers.JoinServerPlayer.OnClientEvent:Connect(function(currentPlayers)
	if not debounce then
		debounce = true
		task.wait(1)
		debounce = false
		
		InServer = true

		for _, plrName in ipairs(currentPlayers) do
			HandlePlayerJoin(plrName)
		end
	end
end)

-- Server Name Update
ReplicatedStorage.ServerNames.OnClientEvent:Connect(function(_, serverName)
	print(serverName .. "PLAYERS SCRIPT")
	myServerName = serverName
end)

-- Create Server Player
ReplicatedStorage.CreateServers.CreateServerPlayer.OnClientEvent:Connect(function(plr)
	HandlePlayerJoin(plr.Name)
	InServer = true
end)

-- Handle Player Names
ReplicatedStorage.PLRNames.OnClientEvent:Connect(function(plrName)
	if InServer then
		task.wait(0.1)
		HandlePlayerJoin(plrName)
	else
		print("Player is not in server")
	end
end)

-- Leave Server Event
ReplicatedStorage.CloseFunction.LeaveServer.OnClientEvent:Connect(function(plrName)
	if Players.LocalPlayer.Name == myServerName then
		print("PlayerNameText is not getting destroyed because the player is the server creator")
	else
		HandlePlayerLeave(plrName)
		InServer = false
	end
end)

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"

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

ReplicatedStorage.CreateServers.CreateServer.OnServerInvoke = function(player)
	print("CreateServer")

	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
	ServerName.Value = player.Name .. "'s SERVER"
	ServerName.Parent = ServerNames

	for _, receivePlayer in ipairs(game.Players:GetPlayers()) do
		ReplicatedStorage.ServerNames:FireClient(receivePlayer, player.Name, ServerName.Value)
	end

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

			local playerServerName = ServerNames:FindFirstChild(player.Name)
			if playerServerName then
				playerServerName:Destroy()
			end

			ReplicatedStorage.CloseFunction.LeaveServer:FireAllClients(player)
		end
	end)

	return ServerID
end

ReplicatedStorage.JoinServers.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)

			ReplicatedStorage.JoinServers.JoinServerPlayer:FireClient(player)

			for _, otherPlayer in ipairs(game.Players:GetPlayers()) do
				ReplicatedStorage.PLRNames:FireClient(otherPlayer, player.Name)
			end

			return true
		end
	end
	warn(player.Name .. " failed to join server with ID:", ServerID)
	return false
end

ReplicatedStorage.CreateServers.CreateServerServer.OnServerEvent:Connect(function(player)
	ReplicatedStorage.CreateServers.CreateServerPlayer:FireAllClients(player)
end)

ReplicatedStorage.CloseFunction.CloseServerUI.OnServerEvent:Connect(function(player)
	ReplicatedStorage.CloseFunction.LeaveServer:FireAllClients(player)
end)
1 Like

Sorry for bump up but I haven’t got a reply in over an hour.

1 Like

you’re only sending one player here. ipairs expects a table

Then what table do i use? (filling character minimum)

Well where do I insert the table in the server script using table.insert?

Is this where I am supposed to put the table?

local playerTable = {}

ReplicatedStorage.CreateServers.CreateServer.OnServerInvoke = function(player)
	print("CreateServer")

	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
	ServerName.Value = player.Name .. "'s SERVER"
	ServerName.Parent = ServerNames

	for _, receivePlayer in ipairs(game.Players:GetPlayers()) do
		ReplicatedStorage.ServerNames:FireClient(receivePlayer, player.Name, ServerName.Value)
		table.insert(playerTable, player.Name)
	end

You are not sending the table on the server so the client is returning nil

Since i assume you are trying to pass the players name, you would do this:

1 Like