Ep 33. Incorrect Pet Upgrade Cost

During this episode we create the pet upgrade system, which allows players who have multiple of the same pet to combine them into a single golden or shiny version from their pet inventory.

It was recently brought to my attention that one additional pet would be consumed when upgrading. To make this easier to understand, let’s say that I have 5 normal Dog pets. I need to have 4 Dog pets in order to upgrade one to golden. Once I perform the upgrade, my 5th dog pet would be consumed as well.

I believe this issue stems from my misunderstanding of how these upgrade systems usually work. When creating this system, I felt that only duplicates of a pet should count towards the upgrade. So if you have a Dog pet and it requires 4 Dog pets to upgrade, then I would actually require the player to have 5 Dog pets in total.

Solution

We need to update our CanUpgradePet function inside of the ReplicatedStorage/Configs/Pets module script.

ReplicatedStorage/Configs/Pets.lua
function Pets.CanUpgradePet(pet: PetInstance, data)
    local nextUpgrade = Pets.GetNextUpgrade(pet)
    if nextUpgrade == "Max Upgraded" then return false end
    
    local duplicatePets = Pets.GetDuplicatePets(pet, data)
    local nextUpgradeCost = Pets.GetNextUpgradeCost(pet, data)
    
    if nextUpgrade == "Shiny" and #duplicatePets >= nextUpgradeCost then
        return true
    elseif nextUpgrade == "Golden" and #duplicatePets >= nextUpgradeCost then
        return true
    end
    
    return false
end

We also need to update our UpgradePet function inside of the ServerScriptService/Pets module script as well.

ServerScriptService/Pets.lua
function Pets.UpgradePet(player: Player, uuid: string)
    local profile = PlayerData.Profiles[player]
    if not profile then return end
    
    local pet: PetsConfig.PetInstance = profile.Data.Pets[uuid]
    if not pet then return end
    
    local canUpgrade = PetsConfig.CanUpgradePet(pet, profile.Data)
    if not canUpgrade then return end
    
    local amountToUpgrade = PetsConfig.GetNextUpgradeCost(pet, profile.Data)
    local duplicatePets = PetsConfig.GetDuplicatePets(pet, profile.Data)
    
    local amountDeleted = 1
    for _, petInstance in duplicatePets do
        if petInstance.UUID == uuid then continue end
        if amountDeleted == amountToUpgrade then break end
        
        Pets.DeletePet(player, petInstance.UUID)
        
        amountDeleted += 1
    end
    
    local nextUpgrade = PetsConfig.GetNextUpgrade(pet)
    
    pet[nextUpgrade] = true
    Remotes.UpgradePet:FireClient(player, uuid, nextUpgrade)    
end
EXPAND
Contents