How doi make a character gui visible and not visible

Good evening , Miszuki here.
In the game that I am working on, ive made a character selection gui.
Now ,what I want is when the client clicks the shop button, it opens and the selection gui appears and when click again it closes.
The issue is, whatever I try is not working for some reason.


.

Ive tried looking for people with similar requests to mine but nobody could match it :frowning:
Any help is much appreciated!

--
 local GUI= CharSelectShop
game.Players.LocalPlayer.Player.Gui.CharSelectShop
local Button = TextButton

locaal function Toggle()
	if CharSelectShop and TextButton then
		GUI.Visible ==true
		GUI.Visible = false
	else
			GUI.Visible = true
	end
end

TextButton.MouseButton1Down:Connect(Toggle)
3 Likes

It is probably because you aren’t doing anything with this:

game.Players.LocalPlayer.Player.Gui.CharSelectShop

It should be:

local Something = game.Players.LocalPlayer.Player.Gui.CharSelectShop

In your Toggle() function, you accidentally added an extra = sign (or you forgot to add this to the if statement?)

GUI.Visible == true --there should only be 1 '=' or put it in an if statement

You also forgot to check to visibility of the button

if CharSelectShop and TextButton then --where are you checking Something.Visible?

You didn’t define CharSelectShop and TextButton. What are they?

2 Likes

That is wrong you have to do :

game.Player.LocalPlayer.PlayerGui

So its like

 local GUI= game.Players.LocalPlayer.PlayerGui.CharSelectShop
local Button = TextButton

locaal function Toggle()
	if CharSelectShop and TextButton then
		GUI.Visible ==true
		GUI.Visible = false
	else
			GUI.Visible = true
	end
end

TextButton.MouseButton1Down:Connect(Toggle)
3 Likes

I made this code, try using it.(Even tho i wasn’t supposed to make this…)

local Gui = game.Players.LocalPlayer:WaitForChild("PlayerGui").Gui.CharSelectShop
local Button = Gui.TextButton --Atleast its what i suppose.

Button.MouseButton1Click:Connect(function()
    wait()
    Gui.Visible = not Gui.Visible
end)

4 Likes

How did I miss that!? Anyways, the OP needs to check the visiblity of the button in the if statement.

if CharSelectShop and TextButton and GUI.Visible then

And why is there a wait()?

1 Like

Another issue is that you’re using a serverscript instead of a localscript which is why LocalPlayer will not work.

EDIT: You also put locaal instead of local for the function so that would’ve also errored

2 Likes

If it turned visible then instantly turned back to invisible, you won’t see it :thinking:

2 Likes

I think the GUI.Visible == true is supposed to be in the if statement but tabbing and other issues caused it to be inside of the codeblock

3 Likes

Oh wait yeah xd. I didn’t read properly.

3 Likes

First and foremost, you’re using a server script. Use a local script when doing things regarding the client.

Also, you’re indexing the GUI all the way from the player, when you can simply use the script’s parent to index it.

The third issue is that you’re doing GUI.Visible
ScreenGui’s do not have a Visible property. Other UI components like Frames and TextButtons do, but ScreenGui’s don’t. However, GUI.Enabled exists, which achieves the same thing, the property name for ScreenGui’s layercollection visibility is simply different from the other ones.

The fourth issue is that you’re checking for the GUI itself in the if statement, meaning you’re checking if it’s nil or not, which shouldn’t be a problem. Because, we can see in the explorer that they are all descendants of PlayerGui.

Another big issue is that you’re using undefined variables and going from Button to TextButton
This isn’t an uncommon issue, make sure that you reference variable names before typing them.

The next thing is that you’re using MouseButton1Down, which isn’t a problem, but it’s recommended to use MouseButton1Click so that the player actually has to click and release MB1 on the button.

local Button = script.Parent
local GUI = Button.Parent:WaitForChild("CharSelectShop")

local function Toggle()
	if GUI.Enabled then
		GUI.Enabled = false
	else
	     GUI.Enabled = true
	end
end

Button.MouseButton1Click:Connect(Toggle)

I may have missed something but I hope this solves your problem and I wish you luck.

The most important thing that I hope for you to take away from this is that server scripts aren’t used for things that run on each clients machine like GUIs.

7 Likes

Thank you for compiling all of the spaghetti answers above into a nice readable format, that should surely help them!

4 Likes

Glad I solved your problem! It’s easy to overlook things like that, don’t worry.

3 Likes

If i’m not mistaken you could shorten the GUI.Enabled if statement down to just one line right?

I think this would work:
GUI.Enabled = not GUI.Enabled
I’m not entirely sure but I think this will switch between true and false making the if statements redundent

3 Likes

Yeah, that works. Just didn’t know if the OP would understand it so I went back to a basic if statement.

2 Likes

Thanks! Your actually a madlad you know? I’m sort of new to scripting so defining guis looks complicated and ill give it look! thanks!

1 Like

Hi @Luacrative , ive tried what you’ve suggested but it doesn’t seem to work.

1 Like

I can see in the explorer that you set it up incorrectly. Should be like this:

CharSelectShop 
   Container 
   CharacterSelect  
       Localscript 

Also I noticed that you put the button inside the GUI when before it was outside. Before it was using the actual GUI and toggling the visibility with enabled but now since the button is also in the frame the code should be changed to this:

local Button = script.Parent
local Frame = Button.Parent:WaitForChild("Container")

local function Toggle()
	if Frame.Visible then
		Frame.Visible = false
	else
	     Frame.Visible = true
	end
end

Button.MouseButton1Click:Connect(Toggle)
2 Likes

just a tip to make code more cleaner
you can just do

local Button = script.Parent
local Frame = Button.Parent:WaitForChild("Container")

local function Toggle()
Frame.Visible = not Frame.Visible
end

Button.MouseButton1Click:Connect(Toggle)
3 Likes

Yeah I know. I want to keep the code simple for the OP to understand.

2 Likes

Ahh it was a mistake , i want the button to be outside of the GUI , and when pressed openw the character select, when pressed again closes it.

2 Likes