Teleport not working (unable to cast Instance to int64)

I am currently trying to make it so that when the player clicks a GuiButton, it teleports them to another place.

Instead, no matter what I do, when I click on the button, even in a live server, it does nothing. When I do it in Studio, it simply says,“Unable to cast Instance to int64” in the Output.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

This is the code (local script) in the button:

local db = false
local lp = game.Players.LocalPlayer
script.Parent.Activated:Connect(function()
	if db == false then
		db = true
		game.ReplicatedStorage.Teleport:FireServer(lp, lp, 6171073179)
	end
	wait(5)
	db = false
end)

This is the server script in the RemoteEvent.

local TeleportService = game:GetService("TeleportService")

game.ReplicatedStorage.Teleport.OnServerEvent:Connect(function(plr, placeid)
	TeleportService:Teleport(placeid, plr)
end)

Is there something wrong with either one of my scripts?

The issue is on this line:

game.ReplicatedStorage.Teleport.OnServerEvent:Connect(function(plr, placeid)

You take in the parameter “placeid”, where as, you fire over the physical id instead of the parameter “place id”.

Instead, make a variable of the place id, and just fire that variable over. Also, why are you firing 2 local players?

game.ReplicatedStorage.Teleport:FireServer(lp, lp, 6171073179)

lp, lp will just fire the same person, you can keep that out and just fire over the id, as the server will automatically pick up the player parameter with it.

local placeid = -- place id
local db = false
local lp = game.Players.LocalPlayer
script.Parent.Activated:Connect(function()
	if db == false then
		db = true
		game.ReplicatedStorage.Teleport:FireServer(placeid)
	end
	wait(5)
	db = false
end)

game.ReplicatedStorage.Teleport.OnServerEvent:Connect(function(player, placeid)
     TeleportService:Teleport(player, placeid)
end)

You can read more about teleporting users here.

Also, instead of going off .Activated, if your going off a UI Button click, use

.MouseButton1Click

The problem is in the local script. The local player is passed twice to the remote event. This is unneccesary as the first parameter of a remote event is always the local player that sent it. That means the OnServerEvent function is taking 3 parameters - plr, plr, plr, and placeid when it only needs to take plr and placeid.

This can be fixed by replacing
game.ReplicatedStorage.Teleport:FireServer(lp, lp, 6171073179)
with
game.ReplicatedStorage.Teleport:FireServer(6171073179)

First, you fire to the server with the LocalPlayer, but there really isn’t a point to that as OnServerEvent tells you which player fired the event. Secondly, the reason it doesn’t work at all is because when you fire the event, you fire three arguments: LocalPlayer, LocalPlayer and 6171073179, which is the placeid.

If you already have a set place to teleport to, I would recommend saving it onto the server, otherwise, if you’d still like to have the client specify the placeid, you’ll have to change a few things.

You’ll want to change the :FireServer line to:

game.ReplicatedStorage.Teleport:FireServer(6171073179) -- We get rid of the two LocalPlayer arguments which is causing this issue.

The reason this works is OnServerEvent automatically, no matter what responds with the player as the first argument, and whatever the client sends through the event is specified afterwards. Heres a demonstration:

--LOCAL SCRIPT

game.ReplicatedStorage.Event:FireServer("hello,", "world!") -- This tells the server two arguments, "hello," and "world!".

--SERVER SCRIPT

game.ReplicatedStorage.Event.OnServerEvent:Connect(function(player, message1, message2) -- First, the player is specified because we know who fired the event.
    print(player.Name.." says: "..message1.." "..message2) -- Prints out the player's message on the server.
end)

-- EXPECTED OUTPUT

"cheeselover_303 says: hello, world!"

I would recommend you read up on RemoteEvents and other Client-to-Server communication methods. Here are some links to those on the developer wiki:

Client to Server communications
Remote Functions
Remote Events

I hope this helps!

Only error in your script is that in the Teleport function, the placeid is written before the player object. It’s otherwise correct, so thanks anyway.

1 Like