Help with this script

So, what I am trying to do is I am trying to make a shop GUI to let the player buy shirts from it. This code will loop through the shirt folder in replicated storage and see if it is in player.ShirtFolder if it isn’t then
it will clone a button and give it custom names and stuff and do button.MouseButton1Click:Connect() But its sort of weird. Have I gotten ahead of myself?

Client:

local ShopShirtEvent = game.ReplicatedStorage.ShopEvent1

local ShirtPrices = require(game.ReplicatedStorage.Modules.ShirtPrices)

local shopguiupdate = game.ReplicatedStorage.ShopGUIUpdate

function ShopShirtUpdate()
	for i, shirts in pairs(game.ReplicatedStorage.Shirts:GetChildren()) do
		if #game.Players.LocalPlayer.ShirtFolder:GetChildren() > 0 then
			for i, button in pairs(script.Parent.Shirts:GetChildren()) do
				if button:IsA("GuiButton") then
					print(button.Name)
					button:Destroy()
				end
			end
			for v, shirt in pairs(game.Players.LocalPlayer.ShirtFolder:GetChildren()) do
				if  shirts ~= shirt and script.Parent.Shirts:FindFirstChild(shirt.Name) == nil then
					local newbutton = game.ReplicatedStorage.BuyButton:Clone()
			
					newbutton.Parent = script.Parent.Shirts
					
					local price = ShirtPrices[shirts.Name].price
			
					newbutton.Name = shirts.Name
					newbutton.Text = "Buy "..shirts.Name.." -"..price
					
					print(newbutton.Name)
		
					newbutton.MouseButton1Click:Connect(function()
						ShopShirtEvent:FireServer(shirts)
					end)
				end
			end
		else
			local newbutton = game.ReplicatedStorage.BuyButton:Clone()
			print(newbutton.Name)

			newbutton.Parent = script.Parent.Shirts
			
			local price = ShirtPrices[shirts.Name].price

			newbutton.Name = shirts.Name
			newbutton.Text = "Buy "..shirts.Name.." - "..price

			newbutton.MouseButton1Click:Connect(function()
				ShopShirtEvent:FireServer(shirts)
			end)
		end
		
		
	end
end

Server Script Storage:

ShopEvent1.OnServerEvent:Connect(function(player, object)
	
	if PriceModule[object.Name].price then
		print("object is valid")
		local price = PriceModule[object.Name].price
		if player.leaderstats.Money.Value > price then
			print(price)
			player.leaderstats.Money.Value -= price
			local newObject = object:Clone()
			newObject.Parent = player.ShirtFolder
			shopguiupdate:FireClient(player)
		end
	end
end)

serverguiupdate just fires the function again.

Here is the position of the script
Capture2

Here is the place where the shirts values are contained at:
Capture3

Here is the result of the script:
robloxapp-20201222-1536000.wmv (458.5 KB)

Here is the output:

Do you need more info? I will prob experiment with it a bit.

1 Like

Fixed it. Gone ahead of myself confirmed.

Code:

function ShopShirtUpdate()
	for i, button in pairs(script.Parent.Shirts:GetChildren()) do
		if button:IsA("GuiButton") then
			print(button.Name)
			button:Destroy()
		end
	end
	for i, shirts in pairs(game.ReplicatedStorage.Shirts:GetChildren()) do
		if game.Players.LocalPlayer.ShirtFolder:FindFirstChild(shirts.Name) == nil then
			local newbutton = game.ReplicatedStorage.BuyButton:Clone()
			print(newbutton.Name)

			newbutton.Parent = script.Parent.Shirts
			
			local price = ShirtPrices[shirts.Name].price

			newbutton.Name = shirts.Name
			newbutton.Text = "Buy "..shirts.Name.." - "..price

			newbutton.MouseButton1Click:Connect(function()
				ShopShirtEvent:FireServer(shirts)
			end)
		end
		
	end
end

I spent yestaday just adding useless code to it. 3 hours of my life wasted. Fixed it in 3 minutes :confused: Study music works.