Issue with backback shop/gui

Alright so, I do not know what is wrong with my code, i’ve been trying to debug it to no avail, it is a backpack equipper that increases a capacity value which allows you to store more coins in game. I have this inside of a localscript which is inside of every button in the store:

local player = game.Players.LocalPlayer

local properties = script.Parent.Parent.Parent.Parent.Main:FindFirstChild("Properties")
local image = properties:FindFirstChild("ImageLabel")
local buy = properties:FindFirstChild("Buy")
local name = properties:FindFirstChild("Name")
local price = properties:FindFirstChild("Price")
local BPC = properties:FindFirstChild("BackpackCapacity")

local ImageID = script.Parent:WaitForChild("Bag").ImageID
local Price = script.Parent.Bag:FindFirstChild("Price")
local BPName = script.Parent.Bag.NameValue.Value
local Capacity = script.Parent.Bag:FindFirstChild("Capacity")

local buyevent = game.ReplicatedStorage.Remotes.Buy
local equipevent = game.ReplicatedStorage.Remotes.Equip

local selected
local pricevar

script.Parent.MouseButton1Down:Connect(function()
	local aselected = BPName
	print(aselected)
	pricevar = Price.Value
	if player.Items:FindFirstChild(BPName) then
		image.Image = "rbxassetid://"..ImageID.Value
		buy.Text = "Owned"
		price.Text = "Price: "..Price.Value
		name.Text = BPName
		BPC.Text = "Capacity: "..Capacity.Value
		
		BPC.TextColor3 = Color3.fromRGB(255, 255, 255)
		price.TextColor3 = Color3.fromRGB(255, 255, 255)
		buy.BackgroundColor3 = Color3.fromRGB(50, 255, 50)
	elseif player.Equipped:FindFirstChild(BPName) then
		image.Image = "rbxassetid://"..ImageID.Value
		buy.Text = "Equipped"
		price.Text = "Price: "..Price.Value
		name.Text = BPName
		BPC.Text = "Capacity: "..Capacity.Value

		BPC.TextColor3 = Color3.fromRGB(255, 255, 255)
		price.TextColor3 = Color3.fromRGB(255, 255, 255)
		buy.BackgroundColor3 = Color3.fromRGB(50, 255, 50)
	else
		image.Image = "rbxassetid://"..ImageID.Value
		price.Text = "Price: "..Price.Value
		name.Text = BPName
		BPC.Text = "Capacity: "..Capacity.Value

		BPC.TextColor3 = Color3.fromRGB(255, 255, 255)
		price.TextColor3 = Color3.fromRGB(255, 255, 255)
		buy.BackgroundColor3 = Color3.fromRGB(50, 255, 50)
		
		local debounce = false

		local piv = player.Items
		local pev = player.Equipped
		local money = player.leaderstats.Money.Value


		buy.MouseButton1Down:Connect(function()
			selected = aselected
			if debounce == false then
				debounce = true
				if pricevar > money then

					print("User does not have enough money.")
					buy.Text = "Not Enough Money"
					wait(1)
					buy.Text = "BUY"
					
				else 
					if piv:FindFirstChild(selected) == nil and pev:FindFirstChild(selected) == nil then
						buyevent:FireServer(selected)
						buy.Text = "Owned"
						selected = nil
					elseif piv:FindFirstChild(selected) ~= nil and pev:FindFirstChild(selected) == nil then
						equipevent:FireServer(selected)
						buy.Text = "Equipped"
						selected = nil
					elseif piv:FindFirstChild(selected) == nil and pev:FindFirstChild(selected) ~= nil then
						selected = nil

				end
				debounce = false
				end
				debounce = false
			end
		end)
	end
end)

(bear in mind im not the greatest scripter so there might have been a better way)

and then there is the remote event script:

local buyevent = game.ReplicatedStorage.Remotes.Buy
local equipevent = game.ReplicatedStorage.Remotes.Equip

buyevent.OnServerEvent:Connect(function(player, selection)
	if player.leaderstats.Money.Value >= game.ReplicatedStorage.Bags:FindFirstChild(selection).Price.Value then
		player.leaderstats.Money.Value = player.leaderstats.Money.Value - game.ReplicatedStorage.Bags:FindFirstChild(selection).Price.Value
		local newlypurchasedbag = game.ReplicatedStorage.Bags:FindFirstChild(selection):Clone()
		newlypurchasedbag.Parent = player.Items
		selection = nil
	end
end)

equipevent.OnServerEvent:Connect(function(player, selection)
	print(selection)
	if #player.Equipped:GetChildren() > 0 then 
		for i, v in pairs(player.Equipped:GetChildren()) do
			if v.Name ~= selection then
				v.Parent = player.Items
			end
		end
	end
	if player.Items:FindFirstChild(selection) ~= nil then
		player.Items:FindFirstChild(selection).Parent = player.Equipped
		player.hiddenleaderstats.Capacity.Value = player.Equipped:FindFirstChild(selection).Capacity.Value
		selection = nil
	end
end)

i have been making the selection var nil because i’ve been trying to find ways to fix the problem

here are some other screenshots that may help:
image
in studio: image

in game: image

script that adds the buttons in game:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BagsFolder = ReplicatedStorage:FindFirstChild("Bags")
local scriptp1 = ReplicatedStorage.Scripts:FindFirstChild("BagShop1")
local sFrame = script.Parent
local Bags = {}

for index, bag in pairs(BagsFolder:GetChildren()) do
   table.insert(Bags, bag)
   
   Button = Instance.new("ImageButton", sFrame)
   
   local imageid = bag:FindFirstChild("ImageID")
   local price = bag:FindFirstChild("Price")
   local capacity = bag:FindFirstChild("Capacity")
   local Name = bag.Name
   
   Button.Image = "rbxassetid://"..imageid.Value
   
   local newscript = scriptp1:Clone()
   newscript.Parent = Button
   
   local bagobj = bag:Clone()
   bagobj.Parent = Button
   bagobj.Name = "Bag"
   
end


if i click each button without clicking buy once at a time, i get 1 print at a time which is expected: image

but once i click buy after clicking all of them: image

and if i equip a bag, such as the better one, it equips a random bag: image

ima go to sleep now so if you need more info it’ll be tomorrow
thanks for any help, I seriously don’t know what i did wrong