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

When I try to remove a GUI it doesn't work and no error message is given, why?

Asked by
Audiimo 105
6 years ago

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)

3 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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)


0
Why do we need to put :GetService()? New to the scripting game so confused about this stuff Audiimo 105 — 6y
0
^That's wrong. GetService is basically the WaitForChild for getting a service like Players or StarterGui. PyccknnXakep 1225 — 6y
0
Also, line 7 wasn't needed. PyccknnXakep 1225 — 6y
1
Okie ^ greatneil80 2647 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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!

Log in to vote
-1
Answered by 6 years ago

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)
0
OMG GOOD POINT greatneil80 2647 — 6y
0
yep rilgundam 65 — 6y
0
idk if ur supposed to get the service of players or not rilgundam 65 — 6y
0
it works both ways ^ greatneil80 2647 — 6y
0
When you don't get a service, your code has a change of erroring, since the service has not been defined yet, but unlike Workspace, Workspace is always defined, so you never need to use :GetService for it. hiimgoodpack 2009 — 6y

Answer this question