I am trying to make a GUI disappear when I click it but nothing happens. I put print("")
to see if it works and I get the first print and not the second one. The StarterGUI is disabled but not the PlayerGUI one. Why?
1 | ImageGui = script.Parent |
2 | OnorOff = game.StarterGui.ScreenGui |
3 |
4 | ImageGui.MouseButton 1 Click:Connect( function (player) |
5 | print ( "This.Works" ) --Prints to see if the code ran through here |
6 | OnorOff.Enabled = false |
7 | game.Players:WaitForChild( "player.Name" ).PlayerGui.ScreenGui.Enabled = false |
8 | print ( "This.Also.Works" ) --Prints to see if the code ran through here |
9 | end ) |
You are removing the GUI but the true GUI is in your player
1 | ImageGui = script.Parent |
2 | --OnorOff = game.StarterGui.ScreenGui -- nope |
3 | OnorOff = game:GetService( "Players" ).LocalPlayer.PlayerGui.ScreenGui -- yes |
4 | ImageGui.MouseButton 1 Click:Connect( function () |
5 | print ( "This.Works" ) --Prints to see if the code ran through here |
6 | OnorOff.Enabled = false |
7 | game:GetService( "Players" ).LocalPlayer.PlayerGui.ScreenGui.Enabled = false |
8 | print ( "This.Also.Works" ) --Prints to see if the code ran through here |
9 | end ) |
First you need to get the player's version of the GUI. This basically means that whatever was in the StarterGui
is cloned into the player's PlayerGui
, so in your script your not indexing the player's version of the GUI.
1 | local imagegui = script.Parent |
2 | local onoroff = game:GetService( "Players" ).LocalPlayer.PlayerGui:FindFirstChild( "ScreenGui" ) |
3 |
4 | imagegui.MouseButton 1 Click:Connect( function () --the 'player' parameter isn't needed. |
5 | onoroff.Enabled = not onoroff.Enabled --not reverses something, so if the player clicks the button once, it disables the gui. when the player clicks it again, it enables gui. you can change this to false if you want to though. |
6 | end ) |
Hopefully this made sense to you, and if it did please accept my answer!
On line 7, I don't think you are supposed to put quotation marks around player.Name. So it should look like this:
1 | ImageGui = script.Parent |
2 | OnorOff = game.StarterGui.ScreenGui |
3 |
4 | ImageGui.MouseButton 1 Click:Connect( function (player) |
5 | print ( "This.Works" ) --Prints to see if the code ran through here |
6 | OnorOff.Enabled = false |
7 | game.Players:WaitForChild(player.Name).PlayerGui.ScreenGui.Enabled = false |
8 | print ( "This.Also.Works" ) --Prints to see if the code ran through here |
9 | end ) |