So I added selling to my game, and its acting very weird. If you sell a mythical, it removes all your legendaries, epics and other mythicals. It’s very buggy. This happens when you have multiple of the same tower in your data.
Heres the local script:
function sellUnit(itemName)
local tower = getTowerByKey(itemName)
if tower then
if tower.Rarity == "Epic" or tower.Rarity == "Legendary" or tower.Rarity == "Mythical" or tower.Rarity == "Exclusive" then
confirmFrame.Visible = true
confirmTitle.Text = "Are you sure you want to sell "..tower.Name
gemsTitle.Text = "For "..tower.SellPrice.." Gems"
confirmYes.Activated:Connect(function()
local canSell = sellFunc:InvokeServer(itemName)
if canSell then
playerData = canSell
updateItems()
confirmFrame.Visible = false
end
end)
confirmNo.Activated:Connect(function()
confirmFrame.Visible = false
sellingUnits = false
sellButton.Text = "Sell Units"
end)
else
local canSell = sellFunc:InvokeServer(itemName)
if canSell then
playerData = canSell
updateItems()
end
end
end
end
And the server script:
sellUnitFunc.OnServerInvoke = function(playerWhoSell,itemName)
print(playerWhoSell,itemName)
local shopItem = getTowerByKey(itemName)
local playerData = data[playerWhoSell.UserId]
if shopItem and playerData then--if it exists and player has some data
if shopItem.Name ~= "Ninja Cameraguy" and shopItem.Name ~= "Supreme Cinemaguy" and shopItem.Name ~= "Big Scientist Cameraguy" and shopItem.Name ~= "Speakergal" and shopItem.Name ~= "Cameraguy" then
local towerToRemove = table.find(playerData.OwnedTowers, itemName)
if towerToRemove then
table.remove(playerData.OwnedTowers, towerToRemove)
playerData.Gems += shopItem.SellPrice
local removeSelected = table.find(playerData.SelectedTowers, itemName)
if removeSelected then
table.remove(playerData.SelectedTowers, removeSelected)
end
end
print(data[playerWhoSell.UserId].OwnedTowers)
replicatedStorage.GuiInteract:FireClient(playerWhoSell)
updateEvent:FireClient(playerWhoSell, data[playerWhoSell.UserId].Gems)
return playerData
elseif shopItem.Name == "Ninja Cameraguy" or shopItem.Name == "Supreme Cinemaguy" or shopItem.Name == "Big Scientist Cameraguy" or shopItem.Name == "Speakergal" then
errorEvent:FireClient(playerWhoSell,"Error","Can't sell gamepass towers!")
end
else
warn("Tower/player data does not exist")
end
return false--if something went wrong
end
I’m really confused on why it’s selling all copies if I’m not looping through. Any help is really appreciated, thanks!