I am currently making a trail shop GUI that allows you to select the item you want to see, preview it, and then possibly buy it with a buy button. It looks like this at the moment:
The imagelabels are all the trails and the preview is on the right. The problem is, when you use the buy button to purchase the previewed item, it purchases every single trail in the trailfolder. How can I fix this?
The first script is a local script located in the shop gui.
local shopFrame = game.StarterGui.Shop:WaitForChild("Frame")
local itemScroller = script.Parent
local itemPreview = script.Parent.Parent.ItemPreview
local trailsFolder = game.ReplicatedStorage:WaitForChild("Trails")
local ownedTrailsFolder = game.Players.LocalPlayer:WaitForChild("OwnedTrails")
local shopFrame = script.Parent.Parent
function updateShop ()
local ownedTrails = ownedTrailsFolder:GetChildren()
for i, trail in pairs(itemsFolder:GetChildren()) do
local name = trail.Name
local price = trail.Price.Value
local desc = trail.ItemDescription.Value
local imagepreview = trail.Folder.ImageLabel
local itemSelection = script.ItemSelection:Clone()
itemSelection.Parent = itemScroller
itemSelection.MouseButton1Click:Connect(function()
itemPreview.Title.Text = name
itemPreview.TextButton.Text = "Buy for " .. price
itemPreview.Desc.Text = desc
itemPreview.PreviewImage.Image = imagepreview.Image
itemPreview.Visible = true
end)
itemPreview.TextButton.MouseButton1Click:Connect(function()
game.ReplicatedStorage.TrailSelectedRE:FireServer(true, trail)
end)
end
end
updateShop()
updateInventory()
ownedTrailsFolder.ChildAdded:Connect(function()
updateShop(); updateInventory()
end)
The next script is a normal script in serverscriptservice:
local trails = game.ReplicatedStorage:WaitForChild("Trails")
game.ReplicatedStorage.TrailSelectedRE.OnServerEvent:Connect(function(plr, buying, item)
print("Message Recieved")
if buying and not plr.OwnedTrails:FindFirstChild(item.Name) then
local price = item.Price.Value
local coins = plr.leaderstats.Coins
if price <= coins.Value then
coins.Value -= price
item:Clone().Parent = plr.OwnedTrails
end
elseif not buying and plr.OwnedTrails:FindFirstChild(item.Name) then
local char = plr.Character
if not char or not char:FindFirstChild("HumanoidRootPart") then return end
for i, child in pairs(char.HumanoidRootPart:GetChildren()) do
if child:IsA("Trail") then child:Destroy() end
end
local newTrail = item:Clone()
newTrail.Attachment0 = char.HumanoidRootPart.TrailTop
newTrail.Attachment1 = char.HumanoidRootPart.TrailBottom
newTrail.Parent = char.HumanoidRootPart
end
end)
Note: If you find any issues with my script that might cause problems in the future, please communicate them with me. Another note is that I did not include some parts of the script for length reasons. If you want to see them, just tell me. If you have any questions, just ask.