Answered by
6 years ago Edited 6 years ago
The problem here is that you are using StarterGui
instead of PlayerGui
. Also out of personal preference, I really do not like using the enabled property of screen guis.
PlayerGui
You may be asking, "why use the player gui?", what is the player gui?", and why can't I see the player gui?".
Well the player gui is an object inside the player that is created when a person joins the game. When that player joins the game, everything in StarterGui gets cloned into the PlayerGui.
The reason you cant see it is because it doesnt exist when you are making the game,only when a player joins(or you use a local server / play solo).
With that said, lets see how you can use the PlayerGui:
1 | local gui = game.Players.LocalPlayer:WaitForChild( "PlayerGui" ):WaitForChild( "MyGui" ) |
2 | local frame = MyGui.Frame |
3 | local button = MyGui.TextButton |
5 | button.MouseButton 1 Click:Connect( function () |
6 | frame.Visible = not Frame.Visible |
Note here that the PlayerGui isn't a service like StarterGui, but an object.
Important Note: modify gui elements in a local script, not a server script
other stuff
While the basics do work, you can add some flare to you on/off mechanism with the tweenPosition function
01 | local gui = game.Players.LocalPlayer:WaitForChild( "PlayerGui" ):WaitForChild( "MyGui" ) |
02 | local frame = MyGui.Frame |
03 | local button = MyGui.TextButton |
06 | button.MouseButton 1 Click:Connect( function () |
08 | frame:TweenPosition(Udim 2. new( 0.3 , 0 , 1 , 0 )) |
10 | frame:TweenPosition(Udim 2. new( 0.3 , 0 ,. 3 , 0 )) |