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

The simplest way to teleport?

I wrote this script to teleport my character to a part (home) on a click of a button.

wait()
char = script.Parent.Parent.Parent.Parent.Character
script.Parent.MouseButton1Click:Connect(function()
	if char.Torso ~= nil then
		char.Torso.Cframe = workspace.home.CFrame
	elseif char.LowerTorso ~= nil then
		char.LowerTorso.CFrame = workspace.home.CFrame
	end
end)

It seems like this doesn't work at all. I tried everything I know, including changing the code to change the position instead of the CFrame and changing the code to change the CFrame/position of the HumanoidRootPart.

Is there something that I'm missing here, or does this entire procedure work?

1 answer

✓ Accepted answer

Try to teleport with HumanoidRootPart and use variable to get player/char.

use local VARIABLE = ... And you can simple use variable to get player, use :FindFirstChild("item") not item ~= nil

Here is fixed script:

First method:

-- Removed the wait()
local player = game.Players.LocalPlayer -- Get player
local char = player.Character or player.CharacterAdded:Wait() -- Get Char

script.Parent.MouseButton1Click:Connect(function()
	-- use HumanoidRootPart not LowerTorso, Torso
	if char:FindFirstChild("HumanoidRootPart") then
        char.HumanoidRootPart.CFrame = workspace.home.CFrame
    end
end)

Second method:

-- (for me this method is better.)
-- Removed the wait()
local player = game.Players.LocalPlayer -- Get player
local char = player.Character or player.CharacterAdded:Wait() -- Get Char

script.Parent.MouseButton1Click:Connect(function()
	char:MoveTo(workspace.home.Position)
end)

Wiki pages:

FindFirstChild()

player.Character

LocalPlayer

CharacterAdded

Hope it helped :D

Solved your problems? put in title [SOLVED] or accept a answer.

11 comments

0

Too many comments, makes it harder to read.

0

shut incaspaz ur just jealous bc he got here first

0

i never even planned on answering this question

0

yeah i bet u didnt u nerd, bc u arent even capable, hehe

0

changed, better now?

0

Should still improve the readability for the first one, by either shortening the comments or removing them

1

now is better?

0

INCAPAble is bad

0

gioni01 wow for someone with high rep i though you'd be more mature

0

" Too many comments, makes it harder to read." this statement doesn't even make any sense what are you talking about

0

I thought this wasn't working. but i figured out that the script needed to be local. A genuine thanks for for the help!

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