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
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)
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)
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)
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.
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
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)
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)