Error with buy system

I have 2 buttons with this local script inside each

local buyButton = script.Parent
local typeOfTrashBag = buyButton.typeoftrashbag.Value
local price = buyButton.price.Value
local holdAmount = buyButton.holdAmount.Value
local purchaseFailedSound = buyButton.Parent.Parent.purchaseFailed
local purchaseCompletedSound = buyButton.Parent.Parent.purchaseCompleted
local equipEvent = game.ReplicatedStorage.Equip
local unequipEvent = game.ReplicatedStorage.UnEquip
local buyEvent = game.ReplicatedStorage.BuyEvent
local player = game.Players.LocalPlayer

local equipped = buyButton.equipped.Value
local owned = buyButton.owned.Value

local function updateButtonState()
if equipped then
buyButton.Text = “unequip”
buyButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
else
if owned then
buyButton.Text = “equip”
buyButton.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
else
buyButton.Text = “buy”
buyButton.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
end
end
end

local function handleButtonClick()
if owned then
if equipped then
equipped = false
unequipEvent:FireServer(price, typeOfTrashBag, holdAmount, equipped, owned)
updateButtonState()
else
equipped = true
equipEvent:FireServer(price, typeOfTrashBag, holdAmount, equipped, owned)
updateButtonState()
end
else
if price <= player.leaderstats.Money.Value then
if equipped then
– Unequip the currently equipped backpack
unequipEvent:FireServer(price, typeOfTrashBag, holdAmount, equipped, owned)
end

  	equipped = true
  	owned = true
  	buyEvent:FireServer(price, typeOfTrashBag, holdAmount, equipped, owned)
  	equipEvent:FireServer(price, typeOfTrashBag, holdAmount, equipped, owned)
  	updateButtonState()
  	purchaseCompletedSound:Play()
  else
  	purchaseFailedSound:Play()
  end

end
end

updateButtonState()

buyButton.MouseButton1Click:Connect(handleButtonClick)
buyButton.TouchTap:Connect(handleButtonClick)

and a script inside serverscript service looking like this

local Equip = game.ReplicatedStorage.Equip
local UnEquip = game.ReplicatedStorage.UnEquip
local buyEvent = game.ReplicatedStorage.BuyEvent

buyEvent.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
player.leaderstats.Money.Value -= price
print(player.leaderstats.Money.Value)
end)

Equip.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
local trashBag = typeOfTrashBag:Clone()
trashBag.Parent = player.Character
player.Character.trashMax.Value = holdAmount
end)

UnEquip.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
if player.Character and player.Character:FindFirstChild(“trashBag”) then
player.Character.trashBag:Destroy()
player.Character.trashMax.Value = 5
end
end)

when i buy a trashbag it auto equips but when i buy another trashbag it auto equips too causing it to break how do i make it so when i buy a trashbag the one i had on previously unequips and equips the new one

example : https://i.gyazo.com/2533d7d3719866063d0863099a3434a4.mp4

before: very exploitable due to people buying bags early with money they dont have (remote event exploits)

local Equip = game.ReplicatedStorage.Equip
local UnEquip = game.ReplicatedStorage.UnEquip
local buyEvent = game.ReplicatedStorage.BuyEvent

buyEvent.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
player.leaderstats.Money.Value -= price
print(player.leaderstats.Money.Value)
end)

Equip.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
local trashBag = typeOfTrashBag:Clone()
trashBag.Parent = player.Character
player.Character.trashMax.Value = holdAmount
end)

UnEquip.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
if player.Character and player.Character:FindFirstChild(“trashBag”) then
player.Character.trashBag:Destroy()
player.Character.trashMax.Value = 5
end
end)

after: not exploitable (unless its a autofarm of sorts)

local Equip = game.ReplicatedStorage.Equip
local UnEquip = game.ReplicatedStorage.UnEquip
local buyEvent = game.ReplicatedStorage.BuyEvent

buyEvent.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
if player.leaderstats.Money.Value >= price then
player.leaderstats.Money.Value -= price
print(player.leaderstats.Money.Value)
end
end)

Equip.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
local trashBag = typeOfTrashBag:Clone()
trashBag.Parent = player.Character
player.Character.trashMax.Value = holdAmount
end)

UnEquip.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
if player.Character and player.Character:FindFirstChild(“trashBag”) then
player.Character.trashBag:Destroy()
player.Character.trashMax.Value = 5
end
end)

also you shouldn’t use a price arg through remote events as thats VERY exploitable too

you can use tables to find the trashbag and then find the price from there

local trashbags = {
[50] = 'Basic' --or something
}

this doesn’t solve your problem, but its just a comment to help you

yooo thanks man how would i use the tables?

so you can go like this

local trashbags = {
[50] = 'Basic' --or something
}

buyEvent.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
if player.leaderstats.Money.Value >= trashbags[typeOfTrashBag] then --trashbags[typeOfTrashBag] gets the price of the trashbag from the table
player.leaderstats.Money.Value -= trashbags[typeOfTrashBag]
print(player.leaderstats.Money.Value)
end
end)

wouldnt this interfere with the destroying of the trashbag because i would have to name the trashbag basic, wooden, etc

NO, you do not have to name it basic

thats was just an example man
image

you can change the string to wtv (trashbag name)

nah nah i get it like when i destroy it it finds the part trashbag in character and destroys it rn this is what it looks like :
image

no, so what im saying is that the value would be found from the remote event variable
example:

local remoteEvent = (what ever it doesnt matter)

remoteEvent:FireServer('CoolTrashBag')
local remoteEvent = (what ever)

local table = {
[50] = 'CoolTrashBag'
}

remoteEvent.OnServerEvent:Connect(function(plr, TrashBagName)
       print('the price of the trashbag is '.. table[TrashBagName] --prints 50
end)

22:47:11.759 ServerScriptService.Trashbag:11: attempt to compare nil <= number - Server - Trashbag:11 gives me this error

can you show me the script? or is it the one i just wrote?

local Equip = game.ReplicatedStorage.Equip
local UnEquip = game.ReplicatedStorage.UnEquip
local buyEvent = game.ReplicatedStorage.BuyEvent

local trashbags = {
[25] = ‘Basic’,
[100] = ‘Wood’
}

buyEvent.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
if player.leaderstats.Money.Value >= trashbags[typeOfTrashBag] then
player.leaderstats.Money.Value -= trashbags[typeOfTrashBag]
print(player.leaderstats.Money.Value)
end
end)

Equip.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
local trashBag = typeOfTrashBag:Clone()
trashBag.Parent = player.Character
player.Character.trashMax.Value = holdAmount
end)

UnEquip.OnServerEvent:Connect(function(player,price,typeOfTrashBag,holdAmount,equipped,owned)
if player.Character and player.Character:FindFirstChild(typeOfTrashBag) then
player.Character.typeOfTrashBag:Destroy()
player.Character.trashMax.Value = 5
end
end)

what are you firing?
if you can send me the local too that would be helpful

local buyButton = script.Parent
local typeOfTrashBag = buyButton.typeoftrashbag.Value
local price = buyButton.price.Value
local holdAmount = buyButton.holdAmount.Value
local purchaseFailedSound = buyButton.Parent.Parent.purchaseFailed
local purchaseCompletedSound = buyButton.Parent.Parent.purchaseCompleted
local equipEvent = game.ReplicatedStorage.Equip
local unequipEvent = game.ReplicatedStorage.UnEquip
local buyEvent = game.ReplicatedStorage.BuyEvent
local player = game.Players.LocalPlayer

local equipped = buyButton.equipped.Value
local owned = buyButton.owned.Value

local function updateButtonState()
if equipped then
buyButton.Text = “unequip”
buyButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
else
if owned then
buyButton.Text = “equip”
buyButton.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
else
buyButton.Text = “buy”
buyButton.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
end
end
end

local function handleButtonClick()
if owned then
if equipped then
equipped = false
unequipEvent:FireServer(price, typeOfTrashBag, holdAmount, equipped, owned)
updateButtonState()
else
equipped = true
equipEvent:FireServer(price, typeOfTrashBag, holdAmount, equipped, owned)
updateButtonState()
end
else
if price <= player.leaderstats.Money.Value then
if equipped then
unequipEvent:FireServer(price, typeOfTrashBag, holdAmount, equipped, owned)
end

  	equipped = true
  	owned = true
  	buyEvent:FireServer(price, typeOfTrashBag, holdAmount, equipped, owned)
  	equipEvent:FireServer(price, typeOfTrashBag, holdAmount, equipped, owned)
  	updateButtonState()
  	purchaseCompletedSound:Play()
  else
  	purchaseFailedSound:Play()
  end

end
end

updateButtonState()

buyButton.MouseButton1Click:Connect(handleButtonClick)
buyButton.TouchTap:Connect(handleButtonClick)

here is where i fire it if thats what u meant

image
image
some images if u were wondering about any of these

tbh im lost rn, i don’t understand why that errored

shouldve just sticked to the main problem and figured it out along the way :cry:

well the problem is that i could break you game with 1 line of code which would GIVE ME MONEY for doing nothing AND the backpack

well what do i do in this situation?!

i think this could help you, as im not very great at explaining, but gnomecode is!