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

How do I use Vector3 in teleportation scripts?

Asked by
Swolcu 2
4 years ago

I'm new to scripting and I am trying to make a teleportation script, but I don't know how to use the position to move the HumanoidRootPart. It says, "Vector3 is not a valid member of MeshPart." Please help.

button = script.Parent.buttonframe.TextButton
gui = script.parent.Frame
telebutton = gui["tele to red"]
platform = game.Workspace.teleplatform
player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
Character = player.Character or player.CharacterAdded:Wait()
torso=  Character:WaitForChild('UpperTorso')

gui.Visible = false

button.MouseButton1Down:connect(function()
    if gui.Visible == false then
        gui.Visible = true

    else
        gui.Visible = false
    end
end)

telebutton.MouseButton1Down:connect(function()
    if gui.Visible == true then
        Character.HumanoidRootPart.Position = torso.Vector3.new("-16.139, 3.998, 49.66")
    end
end)
0
Just letting you know, you don't need to have the quotation marks when you put down Vector3, Udim, CFrame and other number-based values. ChristianTRPOC 64 — 4y

2 answers

Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago

Hello, Swolcu!

When setting an Instance position, you just need to pass an Vector3

button = script.Parent.buttonframe.TextButton
gui = script.parent.Frame
telebutton = gui["tele to red"]
platform = game.Workspace.teleplatform
player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
Character = player.Character or player.CharacterAdded:Wait()
torso=  Character:WaitForChild('UpperTorso')

gui.Visible = false

button.MouseButton1Down:connect(function()
    if gui.Visible == false then
        gui.Visible = true

    else
        gui.Visible = false
    end
end)

telebutton.MouseButton1Down:connect(function()
    if gui.Visible == true then
        Character.HumanoidRootPart.Position = Vector3.new("-16.139, 3.998, 49.66") --removed torso.
    end
end)
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Ok, lets try it!

First Step : First of all we need to know what is the "CFrame" function and how and where we can apply it. So I'll give you a link so you can see how to use the CFrame option.

Link : [https://developer.roblox.com/en-us/api-reference/datatype/CFrame]

Second Step : Second, we need to know where we want to teleport the player. So if we want to teleport it to a Part.Position of some Model then we can use the next pice of code...

local tpPart = workspace:FindFirstChild("TeleportingPart")
local toucTpPart = script.Parent 
--> This script is inserted on the Part we will touch to get teleported.

local function onTouch(hit)
    local character = hit.Parent
        if character then
            character.CFrame = CFrame.new(tpPart.Position) --> We are teleporting the player
        else
            print("The character doesn't exist!")
        end
end

script.Parent.Touched:Connect(onTouch)
--> If the Part is touched then the function will be runned

And that's how we telepor a player to determinated position. But... How we can do this for a click button?

Well... Lets discover it!

Third Step : We need to know the differences between a LocalScript and a Script, in this case we will use a Script, so create it and parent it to the Button.

Fourth Step : Enjoy coding! You always have to enjoy what are you doing! Play some music while you script some lines. Or even you can play a game to be relaxed!

Lets get into the code!

local button = script.Parent 
local tpPart = workspace:FindFirstChild("tpPart") 

--[[
    Change it to whatever your names are. Example "teleportPart". 
    This names are only for a expample
]]

local function teleportFunction(click)
    local player = click.Parent
    if player then
        local character = workspace:WaitForChild(player.Name)
            if character then
                character.CFrame = CFrame.new(tpPart.Position)
end
        end
end

button.MouseButton1Click:Connect(teleportFunction)

local warnMessage = "The code is incorrect!"
pcall(warnMessage)
--> This is all you need for the script!

And that's how we can teleport a player!

I hope my answer already solve your question. If it did please accept my answer, that will help me a lot!

Keep scripting!

Answer this question