Unable to cast value to Object

So I was testing a feature in my game which was the loading screen to teleport to a game. But I ran into a problem. In the output, I saw an error called: Unable to cast value to Object.

This was the script I’ve been using.

local teleportservice = game:GetService("TeleportService")
local event =  game.ReplicatedStorage.Teleport
local id = 1234567890 --i didnt add the game to the experience yet

event.OnServerEvent:Connect(function(player: Player)
	teleportservice:TeleportAsync(id, {player.UserId})
end)

I tried teleporting the player model and the humanoid. But that also gave me errors. So if there is anyway to solve it, that would be great.

1 Like

I’m going to test your code in studio. If I fix it I’ll get back to you!

1 Like

try doing only {player} instead of {player.UserId}

1 Like

Ok. So I found your problem.

It turns out that the second argument of “TeleportAsync” should be a player instance instead of a user id. You can fix your issue by using this code:

local teleportservice = game:GetService("TeleportService")
local event =  game.ReplicatedStorage.Teleport
local id = 1234567890 

event.OnServerEvent:Connect(function(player: Player)
	teleportservice:TeleportAsync(id, {player})
end)
2 Likes

Wow! That took long to discover. Thank you!

2 Likes

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