Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can you teleport a player using a localscript?

I am trying to make a TextButton Where when you press it it tps you back to home base.

But i dont really know how you can do that, can anyone help me with this?

2 answers

✓ Accepted answer

you'll use a RemoteEvent so the server can teleport the player and make the client listen for when the button is clicked.

client:

--inside the TextButton

local remote = game.ReplicatedStorage.RemoteEvent -- mayb this was the remote event
local button = script.Parent

button.Activated:Connect(function()
	remote:FireServer("teleportToHome") -- fire to the server
end)

server:

--located inside ServerScriptService

local remote = game.ReplicatedStorage.RemoteEvent
local spawn = workspace.Spawn -- define where the spawn is for the homebase

remote.OnServerEvent:Connect(function(player, teleportToHome)
	if teleportToHome == "teleportToHome" then
		player.Character:SetPrimaryPartCFrame(spawn.CFrame.Position)

		--[[
if your wondering what the "player" parameter is, its the player or client that called the remote event
		--]]
	end
end)

3 comments

0

thx for the help!

2

No need to use remotes. You can teleport the player from a LocalScript.

0

you dont need to use RemoteEvents for this, and on player.Character:SetPrimaryPartCFrame() you can use player.Character:MoveTo(spawn.Position).

You can use Character:MoveTo(workspace.Spawn.Position) here is a example:

local player = game.Players.LocalPlayer -- Get player
local char = player.Character or player.CharacterAdded:Wait() -- Get player character or wait for player character

local Spawn = workspace:WaitForChild("Spawn") -- Put spawn location here

script.Parent.MouseButton1Click:Connect(function() -- On click in button
	char:MoveTo(Spawn.Position) -- Teleport character to spawn.
end)

Hope it helped :D

Errors? tell-me on comments

Wiki pages:

MoveTo

This discussion is preserved as it was. New answers, comments, and votes are closed.