Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

The simplest way to teleport?

Asked by 5 years ago

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

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

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.
0
Too many comments, makes it harder to read. User#24403 69 — 5y
0
shut incaspaz ur just jealous bc he got here first Gey4Jesus69 2705 — 5y
0
i never even planned on answering this question User#24403 69 — 5y
0
yeah i bet u didnt u nerd, bc u arent even capable, hehe Gey4Jesus69 2705 — 5y
View all comments (7 more)
0
changed, better now? yHasteeD 1819 — 5y
0
Should still improve the readability for the first one, by either shortening the comments or removing them User#24403 69 — 5y
1
now is better? yHasteeD 1819 — 5y
0
INCAPAble is bad Gey4Jesus69 2705 — 5y
0
gioni01 wow for someone with high rep i though you'd be more mature GamingOverlord756 48 — 5y
0
" Too many comments, makes it harder to read." this statement doesn't even make any sense what are you talking about fredfishy 833 — 5y
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! THAIRIN 7 — 5y
Ad

Answer this question