Hello! I am new to the Roblox Dev forum and getting my feet wet with some GUI scripting. I am looking to create a system with interactive purchase buttons.
The issue is that I want these buttons to go from being the purchase label to saying equip and unequip. For my game, you can only have one item in your inventory at a time (that part I have had no issue creating and it works great.) but I want the GUI to reflect that with what item you have equipped.
I’ve tried tactics such as returning a Boolean value in my remote function to alter the buttons and currently believe I have a system going where the already purchased buttons get filtered into their own table and then from that table I want to decide, “If X button is clicked, make it say equipped and make all the other ones says unequipped.”
Again, I may be biting off a bit more than I can chew as an newbie scripter, but it’s something I’ve been taking day by day to understand! Below I have listed the scripts.
This is the server script and what controls the items actually going into the inventory. I don’t believe there’s anything here that would contribute to the solution.
local replicatedStorage = game:GetService("ReplicatedStorage")
local buyEvent = replicatedStorage:WaitForChild("BuyEvent")
local itemFolder = replicatedStorage:WaitForChild("Items")
buyEvent.OnServerInvoke = function(player, buttonPress, buyValue) --Sends values from client button press to the server
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid")
local leaderstats = player.leaderstats
local points = leaderstats.Points
local clonedItem = itemFolder:WaitForChild(buttonPress.Name)
local playerItemsFolder = player:WaitForChild("Player Items")
if not player.Backpack:FindFirstChild(buttonPress.Name) then --If the item is not in the backpack or Player Items Folder, we will begin the purchase.
if not playerItemsFolder:FindFirstChild(buttonPress.Name) then --If the item is not in the Player Items Folder, they don't own it!
if points.Value >= buyValue then --If the player then has enough points to buy the item, lets give it to them!
points.Value -= buyValue --Subtracts points based on the value of the item.
humanoid:UnequipTools() --Avoids the annoying quirk that is equipped tools moving to the workspace.
player.Backpack:ClearAllChildren()
clonedItem:Clone().Parent = playerItemsFolder --Clones it into the Player Items Folder for data storage.
clonedItem:Clone().Parent = player.Backpack --Clones it into the backpack for use!
else
end
else
humanoid:UnequipTools() --Avoids the annoying quirk that is equipped tools moving to the workspace.
player.Backpack:ClearAllChildren()
clonedItem:Clone().Parent = player.Backpack --If they already own the item but don't have it equipped, equip it!
end
else
if clonedItem then --If the cloned item is found in the player's backpack, then remove it!
player.Backpack[buttonPress.Name]:Destroy() --Item is destroyed or, "un-equipped"
end
end
end
Here is the local shop GUI script. What I am attempting to do is filter the purchased items into a separate table that can then be looped through and my idea is one item gets set to equipped while everything else being looped through the table gets set to unequipped.
local replicatedStorage = game:GetService("ReplicatedStorage")
local buyEvent = replicatedStorage:WaitForChild("BuyEvent")
local player = game.Players.LocalPlayer
local playerItemsFolder = player:FindFirstChild("Player Items")
local shopGui = script.Parent
local shopFrame = shopGui:WaitForChild("ShopFrame")
local itemFrames = shopFrame:GetDescendants()
local buttonTable = {}
local purchasedButtonsTable = {}
for _, buttons in pairs(itemFrames) do
if buttons:IsA("IntValue") then
table.insert(buttonTable, buttons.Parent)
end
end
for i, buttonPress in pairs(buttonTable) do
buttonPress.MouseButton1Click:Connect(function()
local selectedButton = buttonPress.Name
local purchasedTag = buttonPress.Bought.Value
local buyValue = buttonPress.Price.Value
local purchaseOutcome = buyEvent:InvokeServer(buttonPress, buyValue)
--print(selectedButton)
if purchasedTag == false then --If items has not been purchased yet run this
if playerItemsFolder:FindFirstChild(buttonPress.Name) then --If item is in Player Items folder we know they own it already
purchasedTag = true --Set the ownership tag to true
if not table.find(purchasedButtonsTable, buttonPress.Name) then --If this item is not in the purchased table yet, add it there.
table.insert(purchasedButtonsTable, buttonPress.Name) --Item is added to list of purchased items.
end
end
end
for i, itemCheck in pairs(purchasedButtonsTable) do
--I think something has to happen here.
print("Looped")
end
print(purchasedButtonsTable)
end)
end
Again, imagine that after clicking a button and purchasing it, the prompt becomes equipped/unequipped and if I equip one tool it says equipped for it and says unequipped for all of the others.
I feel like I’m quite close to the solution but just need a little extra push, or I may need to completely rethink my approach I’m open any and all ideas!