Hi! So I made an inventory and shop system but I am having a small error with this script.
function updateInventory()
for i, child in pairs(invFrame.ShopGUI.Tools:GetChildren()) do
if child:IsA("ImageLabel") then child:Destroy() end
end
local ownedTrails = ownedTrailsFolder:GetChildren()
print(ownedTrails)
table.sort(ownedTrails, function(a, b)
return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
end)
for i, tool in pairs(ownedTrails) do
local item = script.InvItem:Clone()
item.Image = tool.Folder.ImageLabel.Image
item.SelectButton.Text = "Equip"
item.Title.Text = tool.Name
item.Visible = true
item.Parent = invFrame.ShopGUI.Tools
item.SelectButton.MouseButton1Click:Connect(function()
local price = tool.Price.Value
local coins = player.leaderstats.Coins.Value
if item == SelectedTrail then
item.SelectButton.Text = "Equip"
SelectedTrail = nil
else
if player.OwnedTools:FindFirstChild(tool.Name) or (price <= coins) then
item.SelectButton.Text = "Unequip"
else
item.SelectButton.Text = "Unequip"
end
if SelectedTrail then
SelectedTrail.SelectButton.Text = "Equip"
end
SelectedTrail = item
end
TrailSelectedRE:FireServer(tool)
end)
end
end
The error is at:
item.Image = tool.Folder.ImageLabel.Image
It says that Folder is not a valid image of the Gravity Coil when it is.
Picture:
Hey, do you mind helping me with one more issue. So in the inventory frame, you click this unequip button and it triggers the event. Here is the code:
ToolSelectedRE.OnServerEvent:Connect(function(plr, tool)
if not tool or typeof(tool) ~= "Instance" then return end -- we still want this here just in case
local ownedTool = plr.OwnedTools:FindFirstChild(tool.Name)
print("triggered")
if not ownedTool then
print(plr.Name," is buying the ",tool.Name," tool.")
local price = tool.Price.Value
local coins = plr.leaderstats.Coins
if price <= coins.Value then
coins.Value -= price
local newTool = tool:Clone() do
newTool.Parent = plr.OwnedTools
end
end
elseif ownedTool then
if plr.Backpack:FindFirstChild(tool.Name) == nil then
print(plr.Name," equipped ",tool.Name," tool.")
local new = tool:Clone()
new.Parent = plr.Backpack
end
else if plr.Backpack:FindFirstChild(tool.Name) then
plr.Backpack:FindFirstChild(tool.Name):Destroy()
end
end
end)
The print statement at the start of the event is working so that isn’t the issue. It means that its not going through the proper if statement or the Destroy() isn’t working. How can I fix this?
elseif ownedTool then
if plr.Backpack:FindFirstChild(tool.Name) == nil then
print(plr.Name," equipped ",tool.Name," tool.")
local new = tool:Clone()
new.Parent = plr.Backpack
elseif plr.Backpack:FindFirstChild(tool.Name) then
plr.Backpack:FindFirstChild(tool.Name):Destroy()
end
end
end)
Instead of this:
elseif ownedTool then
if plr.Backpack:FindFirstChild(tool.Name) == nil then
print(plr.Name," equipped ",tool.Name," tool.")
local new = tool:Clone()
new.Parent = plr.Backpack
end
else if plr.Backpack:FindFirstChild(tool.Name) then
plr.Backpack:FindFirstChild(tool.Name):Destroy()
end
end
end)
The problem is, if you have two items equipped, only one text button will say unequip instead of both of them. I don’t know what to change in my code to fix this.
Here is my code:
function updateInventory()
for i, child in pairs(invFrame.ShopGUI.Tools:GetChildren()) do
if child:IsA("ImageLabel") then child:Destroy() end
end
local ownedTrails = ownedTrailsFolder:GetChildren()
print(ownedTrails)
table.sort(ownedTrails, function(a, b)
return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
end)
for i, tool in pairs(ownedTrails) do
local item = script.InvItem:Clone()
item.Image = tool:WaitForChild("Folder").ImageLabel.Image
item.SelectButton.Text = "Equip"
item.Title.Text = tool.Name
item.Visible = true
item.Parent = invFrame.ShopGUI.Tools
item.SelectButton.MouseButton1Click:Connect(function()
local price = tool.Price.Value
local coins = player.leaderstats.Coins.Value
if item == SelectedTrail then
item.SelectButton.Text = "Equip"
SelectedTrail = nil
else
if player.OwnedTools:FindFirstChild(tool.Name) or (price <= coins) then
item.SelectButton.Text = "Unequip"
else
item.SelectButton.Text = "Unequip"
end
if SelectedTrail then
SelectedTrail.SelectButton.Text = "Equip"
end
SelectedTrail = item
end
TrailSelectedRE:FireServer(tool)
end)
end
end
The button text changes through this code here:
item.SelectButton.MouseButton1Click:Connect(function()
local price = tool.Price.Value
local coins = player.leaderstats.Coins.Value
if item == SelectedTrail then
item.SelectButton.Text = "Equip"
SelectedTrail = nil
else
if player.OwnedTools:FindFirstChild(tool.Name) or (price <= coins) then
item.SelectButton.Text = "Unequip"
else
item.SelectButton.Text = "Unequip"
end
if SelectedTrail then
SelectedTrail.SelectButton.Text = "Equip"
end
SelectedTrail = item
end
TrailSelectedRE:FireServer(tool)
end)
end
How do I make it so if both items are equipped in the players backpack, both text buttons will say “Unequip”?