How can I make it so player isnt triggering all buttons at once

You can write your topic however you want, but you need to answer these questions:

  1. 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

  2. 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


  3. 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

1 Like

Is this the issue?
you are literally looping through every element in backgrounds and checking if the player has enough coins to buy it all.

--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

yes it is using a for loop to check all templates but I want to make it so only the buy button thats been clicked on and the template its in is triggered

Like you did earlier, you want to add a listener for each item in the backgrounds when looping over them, not checking if the player can buy them all one by one.

-- Assuming you have a TextButton named "myButton" in the workspace, or any other parent instance.
local myButton = game.Workspace.myButton

-- Function to be executed on button click
local function handleClick()
    print("Button clicked!")
    -- Add your desired code here to run when the button is clicked.
end

-- Connect the handleClick function to the MouseButton1Click event of the button
myButton.MouseButton1Click:Connect(handleClick)

What my main issue is how can I even get the button the button is in a template which has been cloned several times btw…

--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("BUTTON HAS BEEN CLICKED!")
						print(frame)
						print(button)
						print(button.Parent)
					end)
				end
			end
		end
	end

This code here is not printing
I am also trying to make it so only the template that has the button the player clicked on is triggered but Idk how to get to the button with out using remote events or for loops and not even that is properly working
v Where the button the player clicks is at v
https://i.gyazo.com/070b7db04f37252f22a584023f63823d.mp4

In a template? If you are cloning the template when the script is a child of the button elements it may result in some undesirable behavior.

The loop should only be once to connect the functions / listeners with each button. Also you should typically not handle purchases on the client as that can allow exploiters to give themselves an unfair advantage over others.

I will try to check what the issue is later and get back to you.