I’m updating one of my games and I’m gonna be adding a Shop Gui but what are the scripts to open and close the shop?
You need to know the GuiObject.Activated event and making UI elements visible. We cannot spoon feed you code so this is about as far as we can help. We can give examples but you can’t copy it.
There are no universal “script codes” for a shop GUI.
You can ask a specific question like “how do I open and close a UI”, which the answer is pretty simple you can toggle the Visible property
Kinda easy:
local RS = game:GetService("ReplicatedStorage")
local OpenShopGui = RS:WaitForChild("OpenShop") -- Remote Event [ReplicatedStorage]
--Warning: This can be a GUI[Button], I used a part just for an reference --
--Server Script --- [Because if ClientSide, a hacker could easily manipulate and open it from anywhere]
game.workspace.ShopButton.ClickDetector.MouseClick:Connect(function(plr)
OpenShopGui:FireClient(plr) --
print"Opened"
end)
Now local Script:
Create a new GUI
Inside GUI create a Background just for aesthetic
Inside Gui create ImageButton
ImageButton name will be : Buy
inside ImageButton create two Values
“StringValue”
Name it “ItemName”
Click on the stringvalue and change the “Value” to “Name of your item here”
local RS = game.ReplicatedStorage
local ChoosedItem = RS:WaitForChild("PurchaseConfirmation")
local OpenShop = RS:WaitForChild("OpenShop")
local ShopGui = playerGui:WaitForChild("Shop")
local Button = script.Parent.Buy -- Reference for the Button
OpenShop.OnClientEvent:Connect(function() -- Will run after the server Fired the specific Remote Event
if ShopGui.Enabled == true then -- If somehow the Shop is [Visible] then
ShopGui.Enabled = false -- Will be Invisibled/Disabled
else if ShopGui.Enabled == false then -- If Disabled, it will Enable / Turn Visible
ShopGui.Enabled = true
end
end
end)
Button.MouseButton1Click:Connect(function(player) --Buy the item after ImageButton is pressed
print"Pressed"
local ItemName = Button.ItemName.Value -- Reference to the "StringValue.Value"
print(ItemName.Name) -- you will see that the Name is what you placed on that Value
ChoosedItem:FireServer(ItemName) -- will fire the server, warning that the player bought an Item
end)
There is some stuff ++ but for now i will only help you with that
Here’s a much simpler version
-- local script
local ShopGUI = referenceToShopGui
local ClickDetector = referenceToClickDetector
ClickDetector.MouseClick:Connect(function()
ShopGui.Visible = ShopGui.Visible and true or false -- funny ternary
end)
Anyway you are assuming they want to open the shop via a ClickDetector.
To open a shop with a guibutton:
-- local script
local button = referenceToGuiButton
local ShopGui = referenceToShopGui
button.MouseButton1Click:Connect(function()
ShopGui.Visible = ShopGui.Visible and true or false -- funny ternary again
end)
To open a shop with a hotkey:
-- local script
local UIS = game:GetService("UserInputService")
local ShopGui = referenceToShopGui
local Hotkey = Enum.KeyCode.E -- can be changed
UIS.InputBegan:Connect(function(input,gameProcessedEvent)
if input.KeyCode == Hotkey and not gameProcessedEvent then
ShopGui.Visible = ShopGui.Visible and true or false
end
end)
Does this actually work? I’ve been using if statements…
Yeah it works it’s a fake ternary expression. You’ve probably seen it in open-source code, it’s just a fast way to write a if statement that sets a value.
Place a Local Script under the close button and copy and paste this inside:
script.Parent.MouseButton1Click:Connect(function()
GUIPATHHERE.Visible = false
end)
local openCloseButton = script.Parent
local frame = script.Parent.Parent
local toggle = false
frame.Visible = false
openCloseButton.MouseButton1Click:Connect(function()
if not toggle then
toggle = true
frame.Visible = true
else
toggle = false
frame.Visible = false
end
end)
This is about as simple as you could make it, here’s the organisation:
When the button is pressed for the first time the “Frame” instance is made visible (and any other elements inside of it are also made visible). Then when the button is next pressed the “Frame” instance is made invisible and any UI instances inside of it are also made invisible.