Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)


4 comments

0

Why do we need to put :GetService()? New to the scripting game so confused about this stuff

0

^That's wrong. GetService is basically the WaitForChild for getting a service like Players or StarterGui.

0

Also, line 7 wasn't needed.

1

Okie ^

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)

5 comments

0

OMG GOOD POINT

0

yep

0

idk if ur supposed to get the service of players or not

0

it works both ways ^

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.