Unable to cast value to object

So my script takes 2 random players from an array and is supposed to teleport them to a place. While the two players are put into the array, the output from the pcall() returns

Unable to cast value to object

How can I fix this?

code:

		local teleportData = {}
		
		for i = 1, 2, 1 do
			local val = randomNumCheck()
			local pickedPlr = playersLookingForMatch[val]
		
			table.insert(playersSelected, #playersSelected + 1, pickedPlr)
			print("Player "..i.." picked: " .. tostring(pickedPlr))
			
			if i == 1 then
				table.insert(teleportData, #teleportData + 1, tostring(pickedPlr))
			elseif i == 2 then
				table.insert(teleportData, #teleportData + 1, tostring(pickedPlr))
			end
		end
		
		local success, result = pcall(function()
			return TeleportService:TeleportPartyAsync(5235588027, playersSelected, teleportData)
		end)
		
		if success then
			local jobId = result
			
			print("Players teleported to "..jobId)
		else
			warn(result)	
		end

nevermind, fixed. I had to change the inserted value to the player object.

table.insert already inserts at #t + 1 when the optional argument isn’t provided,

table.insert(playersSelected, pickedPlr) -- same functionality
	for i = 1, 2, 1 do

The interval will be 1 by default so need to specify that.

			if i == 1 then
				table.insert(teleportData, #teleportData + 1, >tostring(pickedPlr))
			elseif i == 2 then
				table.insert(teleportData, #teleportData + 1, >tostring(pickedPlr))

You can change that to

if i == 1 or i == 2 then

end

instead of an elseif right there.

You already figured out the problem, just clarification I guess.

1 Like