Ranked Matches System | "Unable to cast value to object"

I am attempting for the first time to create a matchmaking system, that teleports 2 random players in game to a set place where they will “battle it out.”

This is the code in the local script:

while true do
	wait(5)
	local myTableName = {}
	
	if #myTableName < 2 then
		local players = game.Players:GetChildren()
		local randomplayer1 = players[math.random(1, #players)]
		local randomplayer2 = players[math.random(1, #players)]
		table.insert(myTableName, randomplayer2)
		table.insert(myTableName, randomplayer1)
		
		wait()
		
		if randomplayer1 == randomplayer2 then
			print("SamePlayer")
			continue
		end

		print(""..randomplayer1.Name.."")
		print(""..randomplayer2.Name.."")
		
		
		local playerIndex = myTableName[math.random(1,#myTableName)]
		local nextplayerIndex = myTableName[math.random(1,#myTableName)]
		repeat wait()
			if nextplayerIndex == playerIndex then
				nextplayerIndex = myTableName[math.random(1,#myTableName)]
			end
		until nextplayerIndex ~= playerIndex


		
		
		print(""..randomplayer1.Name.." is matched with"..randomplayer2.Name.."!")
		
		local playersList = {randomplayer1, randomplayer2}

		--- Teleport players
		
		
		local playert1,playert2 = unpack(playersList)
		script.PartyTP:FireServer(6333006474, playert1,playert2)
		--game:GetService("TeleportService"):TeleportPartyAsync(6333006474, TOTPLAYERS)
		
		for k in pairs (myTableName) do
			myTableName [k] = nil
		end
		continue
		
	end
	
end

And this is the code in the Server Script:

script.Parent.OnServerEvent:Connect(function(ID, playert1,playert2)
	game:GetService("TeleportService"):TeleportPartyAsync(6333006474, playert1,playert2)
end)

I keep getting the error “unable to cast value to object”, which is LINE 2 in the Server Script. Can someone help? Why is this happening?

The second parameter of TeleportPartyAsync is an Array of players to teleport, the function is assuming that playert2 is the third parameter which is supposed to be the data that’s passed in when the teleport is called

Try to do this instead:

script.Parent.OnServerEvent:Connect(function(ID, playert1,playert2)
	game:GetService("TeleportService"):TeleportPartyAsync(6333006474, {playert1, playert2})
end)
1 Like

Dosent work still, has the same error

Wait I see your issue facepalm

OnServerEvent requires the Player who first fired it as the first parameter, you can’t just have your ID variable in there first

Try this:

script.Parent.OnServerEvent:Connect(function(Player, ID, playert1,playert2)
	game:GetService("TeleportService"):TeleportPartyAsync(6333006474, {playert1, playert2})
end)