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.LocalPlayerlocal equipped = buyButton.equipped.Value
local owned = buyButton.owned.Valuelocal 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
endlocal 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)
endequipped = 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
endupdateButtonState()
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.BuyEventbuyEvent.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