Could I get some help, it works in studio, but ingame it'll come up with an error saying "attempt to index field 'LocalPlayer' (a nil value)
function onButton1Down() print("TELEPORTING") game.Players.LocalPlayer.Character.Torso.CFrame = CFrame.new(Vector3.new(35.62, 9.98, -160.93)) end script.Parent.MouseButton1Down:connect(onButton1Down)
As MasterDaniel has said, you can only use LocalPlayer when using a LocalScript. However, since you've got a SurfaceGui in the Workspace, you will be unable to use a LocalScript.
Technically, there is nothing wrong with your code. The only problem is that there is no way to detect who clicked a button with a SurfaceGui in the Workspace since you can't get the player who clicked it from using the MouseButton1Down event, nor can you use a LocalScript outside of the player.
So, what should we do to rectify this issue?
The solution is to parent the SurfaceGui to StarterGui and then use a script to set the Adornee property of the SurfaceGui to the brick that you want the SurfaceGui to go on. The Adornee property basically lets you show a SurfaceGui onto a part without having it parented to the actual part.
After you've done that, just put your code into a LocalScript and if all goes well, the button should work fine and you'll be teleported to the designated position.
I hope my answer has helped you. If it did, be sure to accept it.
You can only use game.Players.LocalPlayer
in a localscript.
As you're using it in a standard script you cannot use this. Instead you can find the player using .Parent a bunch.
function onButton1Down() print("TELEPORTING") script.Parent.Parent.Character.Torso.CFrame = CFrame.new(Vector3.new(35.62, 9.98, -160.93)) -- Add as many parents as it takes you go get to player. Remember that the hierarchy will be ScreenGui -> PlayerGui -> Player. end script.Parent.MouseButton1Down:connect(onButton1Down)