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?
ImageGui = script.Parent OnorOff = game.StarterGui.ScreenGui ImageGui.MouseButton1Click:Connect(function(player) print("This.Works") --Prints to see if the code ran through here OnorOff.Enabled = false game.Players:WaitForChild("player.Name").PlayerGui.ScreenGui.Enabled = false print("This.Also.Works") --Prints to see if the code ran through here end)
You are removing the GUI but the true GUI is in your player
ImageGui = script.Parent --OnorOff = game.StarterGui.ScreenGui -- nope OnorOff = game:GetService("Players").LocalPlayer.PlayerGui.ScreenGui -- yes ImageGui.MouseButton1Click:Connect(function() print("This.Works") --Prints to see if the code ran through here OnorOff.Enabled = false game:GetService("Players").LocalPlayer.PlayerGui.ScreenGui.Enabled = false print("This.Also.Works") --Prints to see if the code ran through here 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.
local imagegui = script.Parent local onoroff = game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ScreenGui") imagegui.MouseButton1Click:Connect(function() --the 'player' parameter isn't needed. 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. 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:
ImageGui = script.Parent OnorOff = game.StarterGui.ScreenGui ImageGui.MouseButton1Click:Connect(function(player) print("This.Works") --Prints to see if the code ran through here OnorOff.Enabled = false game.Players:WaitForChild(player.Name).PlayerGui.ScreenGui.Enabled = false print("This.Also.Works") --Prints to see if the code ran through here end)