You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? I’m tryin to make it so the Button thats been Clicked which is within a template thats added based on the item in a folder in Replicated Storage
-
What is the issue? When I click on one of the buy buttons it buys all the items in the shop at once and spends around 700 coins which is what all of it values up to… I am using for loops but if there is a way to make it so the button the player clicks on is triggered and nothing else that would be perfect
-
What solutions have you tried so far? I have tried using another Remote Event from within the script and trying to get the button from within the cloned Template but that hasn’t worked so only solution I have left is Instead of cloning the templates for each item in the shop folder I will make the templates in the shop UI and have a script connected with each of the buttons… It would be a bit of code rework tho…
GameShopBuyButtonScript (Script)
local buyiteml = game.ReplicatedStorage:WaitForChild("BuyItem")
local tools = game.ReplicatedStorage.ShopTools:GetChildren()
local backgrounds = game.ReplicatedStorage.ShopBackgrounds:GetChildren()
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(arg)
--Get Players Leaderstats
local stat = arg:FindFirstChild('leaderstats')
if not stat then return end
--Get players Inventory
local inventory = arg.PlayerGui.InventoryandHotbar.Inventory
--Purchase tools
for i, tool in pairs(tools) do
local toolcost = tool:WaitForChild("CostValue")
warn("st")
if stat.Coins.Value < toolcost.Value then --error here
print("not enough coins (Purchase Declined)")
--return
elseif stat.Coins.Value >= toolcost.Value then
print("Purchased!")
stat.Coins.Value -= toolcost.Value
end
local clone = tool:Clone()
clone.Parent = inventory.Items.Grid
warn("nd")
end
--Purchase backgrounds
for i, background in pairs(backgrounds) do
local backgroundcost = background:WaitForChild("CostValue")
if stat.Coins.Value < backgroundcost.Value then
print("not enough coins (Purchase Declined)")
--return
elseif stat.Coins.Value >= backgroundcost.value then
print("Purchased!")
stat.Coins.Value -= backgroundcost.Value
end
local clone = background:Clone()
clone.Parent = inventory.Items.Grid
end
warn("1")
--Purchase Button
local mainshopframeitems = arg.PlayerGui.GameShopGui.BackFrame.MainShopFrame:GetChildren() --Make it so only the button player clicks on
for _, frame in ipairs(mainshopframeitems) do
if frame:IsA("Frame") then
for b, button in pairs(frame:GetChildren()) do
if button:IsA("TextButton") then
button.MouseButton1Click:Connect(function()
print(frame)
print(button)
print(button.Parent)
end)
end
end
end
end
warn("2")
--return true
end)
If you want to see my script that clones the templates and retrieves the data from Rep Storage is below vVv
GameShopScript (LocalScript)
local folder = game.ReplicatedStorage:WaitForChild("ShopTools"):GetChildren()
local secondfolder = game.ReplicatedStorage:WaitForChild("ShopBackgrounds"):GetChildren()
local backframe = script.Parent:WaitForChild("BackFrame")
local frame = backframe:WaitForChild("MainShopFrame")
local template = frame.UIGridLayout:WaitForChild("ShopItemTemplate")
local sound = game:GetService("SoundService")
local iteminfo = script.Parent.InfoBackframe
for i, item in next, folder do
--Clone template
local newTemplate = template:Clone()
newTemplate.Name = item.Name
newTemplate.PriceLabel.Text = item:WaitForChild("Cost").Value
newTemplate.Visible = true
newTemplate.Parent = frame
--Tool vpf
local object = item:Clone()
object.Parent = newTemplate.ViewportFrame
if item:IsA("Tool") then
local camera = Instance.new("Camera")
camera.CFrame = CFrame.new(object.Handle.Position + (object.Handle.CFrame.lookVector*0)+Vector3.new(1,2,3),object.Handle.Position)
camera.Parent = newTemplate.ViewportFrame
newTemplate.ViewportFrame.CurrentCamera = camera
end
--Item Info
if newTemplate:IsA("Frame") then
newTemplate.MouseEnter:Connect(function()
iteminfo.Visible = true
iteminfo.ItemName.Text = item.Name
iteminfo.ItemDescription.Text = item:WaitForChild("Description").Value
end)
newTemplate.MouseLeave:Connect(function()
iteminfo.Visible = false
end)
end
end
for i, item in next, secondfolder do
--Clone template
local newTemplate = template:Clone()
newTemplate.Name = item.Name
newTemplate.PriceLabel.Text = item:WaitForChild("Cost").Value
newTemplate.Visible = true
newTemplate.Parent = frame
--Background vpf
local object = item:Clone()
object.Parent = newTemplate.ViewportFrame
newTemplate.BackgroundImage.ImageTransparency = 0
newTemplate.BackgroundImage.Image = "rbxassetid://"..item.ImageID.Value
--Item Info
if newTemplate:IsA("Frame") then
newTemplate.MouseEnter:Connect(function()
iteminfo.Visible = true
iteminfo.ItemName.Text = item.Name
iteminfo.ItemDescription.Text = item:WaitForChild("Description").Value
end)
newTemplate.MouseLeave:Connect(function()
iteminfo.Visible = false
end)
end
end
--Purchase Button
for items, item in pairs(frame:GetChildren()) do
if item:IsA("Frame") then
local buybutton = item.BuyButton
print(buybutton)
buybutton.MouseButton1Click:Connect(function()
sound.ScreenUISFX.ButtonClickSFX:Play()
print("Purchase Button has been clicked!")
print(buybutton.Parent)
end)
end
end
If theres any Solutions to the For Loop or anything else pls tell me in the replies below! Thx