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

How to make closee and open Button for GUI?

Asked by 6 years ago

So i made a shop gui and i got problems with it , it doesnt work any help?

script for open button :

game.StarterGui.Shop.Open.MouseButton1Click:connect(function() game.StarterGui.Shop.ShopFrame.Visible = true end)

script for close button :

game.StarterGui.Shop.ShopFrame.Close.MouseButton1Click:connect(function() game.StarterGui.Shop.ShopFrame.Visible = false end)

2 answers

Log in to vote
0
Answered by 6 years ago

Problem with your code


Ok, the confusion between PlayerGui and StarterGui is a common mistake made by many developers. The PlayerGui is what should be used to modify the GUIs in game while the StarterGui should be used to place things that will be cloned into the PlayerGui when the player joins a game.


Ways to Fix


There are a couple ways to fix this, those being Either using a local script inside a gui element or using game.Players.LocalPlayer.PlayerGui.

Ex 1(LS inside gui element):

local gui = script.Parent
local other_gui = gui.Parent.OtherGui

gui.Mousebutton1Click:Connect(function()
    other_gui.Visible = not other_gui.Visible
end)

Ex 2(Getting the PlayerGui):

local plrgui = game.Player.LocalPlayer:WaitForChild("PlayerGui")
local frame = plrgui.ScreenGui.Frame
local other_frame = plrgui.ScreenGui.OtherFrame 

gui.Mousebutton1Click:Connect(function()
    other_frame.Visible = not other_frame.Visible
end)

Spicing up your game


While simply switching the visible property of your frame is sufficient, we can add a bit of pizzazz with TweenPosition

local gui = script.Parent
local other_gui = gui.Parent.OtherGui
local open = false

gui.Mousebutton1Click:Connect(function()
    if open then
        other_gui:TweenPosition(Udim2.new(.5,0,1,0))
    else
        other_gui:TweenPosition(Udim2.new(.5,0,0,0))
    end
end)
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The reason this doesn't work is that you're using StarterGui. The StarterGui object is meant to copy the contents of it to every player, changes cannot be made if you edit from StarterGui.

Look at this if you still don't understand.

"When you make a new Roblox game, this screen GUI space doesn’t exist — it’s your job to add it. The easiest way is to add it to the StarterGui service so that it gets copied to a player’s local game session when they join the game."

The code for those scripts shouldn't be referring to StarterGui. The best and easiest way to do this is just parent the local scripts into the required objects, one inside the close button, and the other inside the button that opens the shop. You should then use script.Parent instead of the objects inside StarterGui. I'll give you the new fixed script based on what you have.

Here's some expected hierarchy. Anything going forward is a child of that object.

--[[
    StarterGui
        Shop
            Open
                OpenScript (LocalScript)
            ShopFrame
                Close
                    CloseScript (LocalScript)
                (and whatever else is here)
]]--

And now here's the new scripts that should be fixed. Close script:

script.Parent.MouseButton1Click:Connect(function ()
    script.Parent.Parent.Visible = false
    -- The second "Parent" should refer back to the shop frame since the script is inside the close button
end)

Open script:

script.Parent.MouseButton1Click:Connect(function ()
    script.Parent.Parent.ShopFrame.Visible = true
    -- "Parent.Parent" goes back to the whole GUI object then tells the ShopFrame to be visible from there.
end)

If this helped you, remember to mark this answer as Solved.

(BTW if you think I'm spoon-feeding someone code, you're just wrong. I simply explained to the poster and gave him the new fixed script.)

EDIT: I just noticed someone already gave an answer, but I hope this helped too. (Probably not since you already accepted someone else but ok XD)

Answer this question