Unable to cast value to Objects - TeleportService

I have tried by sending a remote event to the sever which just came up with the same error (Unable to cast value to Objects) so i tried in a local script again but it doesnt work

heres the LocalScript:

local tps = game:GetService("TeleportService")
local player = game.Players.LocalPlayer
local M1Button = script.Parent

M1Button.MouseButton1Click:Connect(function()
	task.wait(1)
	tps:TeleportAsync(PlaceID, player)
	task.wait(1)
end)

You need an array not a player:
tps:TeleportAsync(PlaceID, {player})

TeleportAsync requires a Table of Players, what you could do which might is just

	tps:TeleportAsync(PlaceID, {player})

3 Likes

You should use the Teleport method since you are requesting a teleport in a new thread (which is the function that is contained under the connection)

Make sure that PlaceID is a number, the number should be the ID of the new game you’d like to teleport the player to, I’ll use a placeholder variable for convenience.

Since the script is a LocalScript; you do not need to provide the player that you are teleporting because the game will automatically determine that the LocalPlayer should be teleported.

local teleportService = game:GetService('TeleportService')
local localPlayer = game:GetService('Players').LocalPlayer
local guiButton = script:FindFirstAncestorWhichIsA('GuiButton')
local placeId = 0

guiButton.MouseButton1Up:Connect(function(x: number, y: number)
	teleportService:Teleport(placeId)
end)

The changes I’ve made to the code include the usage of camelCase, finding the ‘GuiButton’ class through ancestors, switching to MouseButton1Up as the signal to connect to because most modern UI typically expects the client to somewhat confirm their action by releasing the click they’ve initiated on that button. I’ve also simplified the logic by removing unnecessary task.wait calls, while maintaining functionality.

2 Likes

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