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

GUI Button Script, supposed to Delete GUI and teleport player, how to?

Asked by 6 years ago

Im making this game on roblox, and I cant seem to find a way to make my button teleport the player. This is the code I want to put the teleport into:

local function OnClicked()
    script.Parent.Parent:Remove()
end

script.Parent.MouseButton1Click:connect(OnClicked)

Its part of a menu, where when you click the button, its supposed to close out the GUI and teleport you to an XYZ coordinates. I have the part to close out the GUI, but I cant find out how to make to teleport the player!

1 answer

Log in to vote
2
Answered by
Newrown 307 Moderation Voter
6 years ago
Edited 6 years ago

To do this there is a very useful method called MoveTo which can be called on any model.

MoveTo takes one parameter which is the Vector3 position

Here you can call MoveTo on the player character's model to move it to a certain Vector3 position

A way to get the player and the player's character in a local script, and only in a local script is by game.Players.LocalPlayer and then game.Players.LocalPlayer.Character to get the player's character

So since this is a local script, we can set up the variables like this:

Note: using the localplayer method only works for local scripts

local player = game.Players.LocalPlayer 
local character = player.Character 

Now that you set up the variables you can go ahead and move the player's character

local function OnClicked()
    script.Parent.Parent:Destroy() -- remove is deprecated, use Destroy instead
    character:MoveTo(Vector3.new(0, 5, 10)) -- assuming this is the vector3 position
end

script.Parent.MouseButton1Click:connect(OnClicked)

Hope it helped!

0
Thanks! It took me a minute to find out how it worked, but I finally found out how it worked, thanks! Zeref_DragneelFT 8 — 6y
0
Happy it helped :) Newrown 307 — 6y
Ad

Answer this question