Need help with error when invoking server with function

I am making a lobby system that allows players to create a lobby or join lobbies others have created. I already have a working creation system and listing system. Now I am just trying to make a script that allows players to click a join button to join a listed server.

My problem is that when I invoke this remote function, I am trying to send the name of the server that they are trying to join with it. However, when I do this, I get this error: “Unable to cast value to Object”.
image

This script is a local script that is parented to the join button of a listed server.

local JoinServer = workspace.Events.JoinServer
local JoinButton = script.Parent.JoinButton
JoinButton.MouseButton1Click:Connect(function(plr)
	local ServerName = JoinButton.Parent.Name
	
	for i, Server in pairs(ServerFolder:GetChildren()) do
		if Server.Name == ServerName then
			local ServerOwnerName = Server.ServerOwner.Value
			local ServerOwnerUserId = PlayerService:GetUserIdFromNameAsync(ServerOwnerName)
			print(ServerOwnerUserId, "UserID")
			local Success = JoinServer:InvokeServer(ServerName)
			if Success == true then

The line that causes the error is local Success = JoinServer:InvokeServer(ServerName)
This is not the entire script, but I feel this is what is necessary to share.

Show us the line that caused the error, or is the :InvokeServer line the one that’s causing the error?

Yes, the line local Success = JoinServer:InvokeServer(ServerName) is causing the error.

Show the full stack trace, and try sending a string constant instead of the variable

More info is needed. Is this a Server error, a Client error? What is the implementation on the server?

1 Like

When I send a string it works just fine. If I send a variable containing the string though it comes out with an error again. I’m not entirely sure what a stack trace is but I think you are asking for this:

This error happens on the Client. The Client is supposed to send the server the name of the lobby the player is attempting to join. Here is the server side of the function. Please keep in mind that I am not yet done with this part as I am trying to resolve the current issue.

JoinServer.OnServerInvoke = function(plr, ServerName)
	print(ServerName)
	local AmountOfPlayers = 0
	local Success = false
	for i, Server in pairs(ServerFolder:GetChildren()) do
		print(ServerName, Server.Name)
		if ServerName == Server.Name then
			for i, PlayerAmount in pairs(Server.Players:GetChildren()) do
				AmountOfPlayers += 1
			end
			print("True 2")
			if AmountOfPlayers < 2 then
				local AddPlayer = Instance.new("StringValue")
				AddPlayer.Parent = Server.Players
				AddPlayer.Name = "Player2"
				AddPlayer.Value = plr.Name
				local AddedPlayer = plr
				PlayerAdded:FireClient(ServerName, AddedPlayer)
				Success = true
			else
				Success = false
			end
		end
	end
	return Success
end

There is one thing I found… MouseButton1Click doesn’t work like that…

JoinButton.MouseButton1Click:Connect(function() ... end)

This is how you do it. To get the player who clicked, it is local plr game.Players.LocalPlayer. for your instance…
Everything else however looks fine…

I fixed the one issue you mentioned here. Could it have something to do with the remotefunctions being in workspace?

When you :FireClient() the first object have to be the player object

This is the error line:

PlayerAdded:FireClient(ServerName, AddedPlayer)

Add Player as the first parameter.

2 Likes

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