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

GUI trouble help?

Asked by 9 years ago

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)
0
Is this in a LocalScript or a Script? Spongocardo 1991 — 9y
0
Serverscript AbsoluteZeroDEV 45 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

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.

Ad
Log in to vote
0
Answered by 9 years ago

You can only use game.Players.LocalPlayerin 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)
0
Well actually my GUI is a SurfaceGUI in workspace, it goes Part -> SurfaceGui -> Frame -> TextButton -> Script so I don't know how many .parents I'll need AbsoluteZeroDEV 45 — 9y

Answer this question