Upgrade Pet Level

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to Modify my script, I just don’t know how. from simple pet system to level/upgrade pet via purchase that also includes rarity “Common” “Mid” and “Rare”
  2. What is the issue? Include screenshots / videos if possible!
    I want to upgrade pet level via ingame shop Purchase
    image
    when player purchase the initial Price(level1) (shown in image) the button will turn into “upgrade for $” (level2) if purchase again to lvl 3 button will turn into “Max upgrade”
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried a lot but seems my logic is far from what it should be.
this is my hierarchy in Replicated storage
image
I tried to make customizePet folder that includes the IntValue Level , level cost per upgrade then the Rarity so I can have a reference (but I think I’m doing it wrong) inside the lvl1 lvl2 lvl3 folder is the model per upgrade.

Server Script

game.ReplicatedStorage.PetSelectedRE.OnServerEvent:Connect(function(plr, buying, pet)
	if buying and pet and not plr.PetInventory:FindFirstChild(pet.Name) then
		print("Buying pet:", pet.Name)

		local price = pet.CustomizePet.Price.Value -- Assuming the price is based on the pet's rarity
		local money = plr.leaderstats.Currency.Value

		print("Price:", price)
		print("Money:", money)

		if price <= money then
			money -= price

			local petFolder = game.ReplicatedStorage.Pets:FindFirstChild(pet.Name)

			if petFolder then
				local levelFolder = petFolder:FindFirstChild("Lvl1")
				local model = levelFolder and levelFolder:FindFirstChildWhichIsA("Model")

				if model then
					local newPet = model:Clone()
					newPet.Parent = plr.PetInventory

					print("Pet successfully bought and added to inventory.", pet.Name)
					print("Inventory contents:")
					for _, pet in pairs(plr.PetInventory:GetChildren()) do
						if pet:IsA("Model") then
							print(pet.Name)
						end
					end

					local PetBoughtRE = game.ReplicatedStorage.Remotes.PetBought
					PetBoughtRE:FireClient(plr, newPet)
				else
					print("Model not found for pet:", pet.Name)
				end
			else
				print("Pet folder not found:", pet.Name)
			end
		else
			print("Insufficient funds to buy the pet.")
		end

	elseif buying and plr.PetInventory:FindFirstChild(pet.Name) then
		local petFolder = plr.PetInventory:FindFirstChild(pet.Name)

		if petFolder then
			local petLevel = petFolder.CustomizePet:FindFirstChild("Level")

			if petLevel and petLevel.Value == 1 then
				local petInfo = game.ReplicatedStorage.Pets:FindFirstChild(pet.Name)

				if petInfo then
					local level2Folder = petInfo:FindFirstChild("Lvl2")
					local level2Cost = level2Folder and level2Folder.CustomizePet and level2Folder.CustomizePet.level2.Value
					local money = plr.leaderstats.Currency.Value

					if level2Cost and level2Cost <= money then
						money -= level2Cost

						local clone = petFolder:Clone()
						petFolder:Destroy()

						clone.CustomizePet.Level.Value = 2
						clone.Parent = plr.PetInventory

						print("Pet successfully upgraded to Level 2.")
					else
						print("Insufficient funds to upgrade pet to Level 2.")
					end
				else
					print("Pet info not found:", pet.Name)
				end
			elseif petLevel and petLevel.Value == 2 then
				local level3Cost = game.ReplicatedStorage.Pets[pet.Name].CustomizePet.level3.Value -- Assuming the cost for leveling up from level 2 to level 3
				local money = plr.leaderstats.Currency.Value

				if level3Cost and level3Cost <= money then
					money -= level3Cost

					local clone = petFolder:Clone()
					petFolder:Destroy()

					clone.CustomizePet.Level.Value = 3
					clone.Parent = plr.PetInventory

					print("Pet successfully upgraded to Level 3.")
				else
					print("Insufficient funds to upgrade pet to Level 3.")
				end
			end
		else
			print("Pet folder not found in player's inventory:", pet.Name)
		end

	elseif not buying and plr.PetInventory:FindFirstChild(pet.Name) then
		local char = plr.Character

		if not char or not char:FindFirstChild("HumanoidRootPart") then
			return
		end

		for i, child in pairs(char.HumanoidRootPart:GetChildren()) do
			if child:IsA("Model") then
				child:Destroy()
			end
		end

		local newPet = pet:Clone()
	
	end
end)

client side

function updateInventory()
	for i, child in pairs(invFrame.PetScroller:GetChildren()) do
		if child:IsA("ImageLabel") then
			child:Destroy()
		end
	end

	local ownedPets = {}

	-- add only owned pets to the table
	for i, pet in ipairs(petFolder:GetChildren()) do
		if pet:IsA("Model") and pet:FindFirstChild("Price") and petFolder:FindFirstChild(pet.Name) and game.Players.LocalPlayer:FindFirstChild("PetInventory") then
			local petInventory = game.Players.LocalPlayer.PetInventory
			if petInventory:FindFirstChild(pet.Name) then
				table.insert(ownedPets, pet)
			else
				 print("Pet not found in inventory:", pet.Name)
				print("Inventory contents:", petInventory:GetChildren())
				print(pet.Name)
			end
		end
	end
	




	table.sort(ownedPets, function(a, b)
		local priceA = a:FindFirstChild("CustomizePet") and a.CustomizePet:FindFirstChild("Price")
		local priceB = b:FindFirstChild("CustomizePet") and b.CustomizePet:FindFirstChild("Price")
	end)

	for i, pet in pairs(ownedPets) do
		local item = script.Item:Clone()
		item.SelectButton.ButtonText.Text = "EQUIP"
		item.PetName.Text = pet.Name
		item.Parent = scrollingFrame

		local newPet = pet:Clone()
		newPet.Parent = item.ViewportFrame

		if newPet.PrimaryPart then
			local camera = Instance.new("Camera")
			camera.CFrame = CFrame.new(newPet.PrimaryPart.Position + Vector3.new(0, 5, 10), newPet.PrimaryPart.Position)
			camera.Parent = item.ViewportFrame
			item.ViewportFrame.CurrentCamera = camera
			camera.FieldOfView = 10
		else
			warn("PrimaryPart not found for pet:", pet.Name)
		end

		if game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart"):FindFirstChild(pet.Name) then
			item.SelectButton.ButtonText.Text = "EQUIPPED"
		end

		item.SelectButton.MouseButton1Click:Connect(function()
			game.ReplicatedStorage.Remotes.EquipPet:FireServer(pet.Name)
			for _, itemButton in pairs(item.Parent:GetChildren()) do
				if itemButton:IsA("ImageLabel") then
					itemButton.SelectButton.ButtonText.Text = "EQUIP"
				end
			end
			item.SelectButton.ButtonText.Text = "EQUIPPED"
		end)
	end
end






function updateShop()
	for i, child in pairs(shopFrame.PetScroller:GetChildren()) do
		if child:IsA("ImageLabel") then
			child:Destroy()
		end
	end

	local PetShop = petFolder:GetChildren()

	table.sort(PetShop, function(a, b)
		local priceA = a:FindFirstChild("CustomizePet") and a.CustomizePet:FindFirstChild("Price")
		local priceB = b:FindFirstChild("CustomizePet") and b.CustomizePet:FindFirstChild("Price")

		if priceA and priceB then
			return priceA.Value < priceB.Value or (priceA.Value == priceB.Value and a.Name < b.Name)
		elseif priceA then
			return true
		elseif priceB then
			return false
		else
			return a.Name < b.Name
		end
	end)


	for i, pet in pairs(PetShop) do
		local item = script.Item:Clone()

		------------additional code for viewport

		local petFolder = game.ReplicatedStorage.Pets:FindFirstChild(pet.Name)
		local levelFolder = petFolder and petFolder:FindFirstChild("Lvl1")
		if levelFolder then
			local newPet = levelFolder:FindFirstChildWhichIsA("Model")
			if newPet then
				newPet = newPet:Clone()
				newPet.Parent = item.ViewportFrame

		if newPet:IsA("Model") and newPet.PrimaryPart then
			local camera = Instance.new("Camera")
			camera.CFrame = CFrame.new(newPet.PrimaryPart.Position + Vector3.new(0, 5, 10), newPet.PrimaryPart.Position)
			camera.Parent = item.ViewportFrame
			item.ViewportFrame.CurrentCamera = camera
			camera.FieldOfView = 10
				end
			end
		end
		

		local selectButton = item:FindFirstChild("SelectButton")
		local price = pet:FindFirstChild("CustomizePet") and pet.CustomizePet:FindFirstChild("Price")
		selectButton.ButtonText.Text = price and "BUY for " .. price.Value or "N/"

		-----------------------
		local customizeFolder = PetInventory:FindFirstChild(pet.Name)
		--local customizePet = customizeFolder:FindFirstChild("CustomizePet")
		--local level = customizePet and customizePet.Level.Value
		
		if customizeFolder then
			local customizePet = customizeFolder:FindFirstChild("CustomizePet")

			if customizePet then
				local level = customizePet.Level.Value
				local level2Cost = customizePet.level2.Value
				local level3Cost = customizePet.level3.Value

				if level == 1 then
					selectButton.ButtonText.Text = "Upgrade for " .. level2Cost
					selectButton.MouseButton1Click:Connect(function()
						game.ReplicatedStorage.PetSelectedRE:FireServer(true, pet)
					end)
				elseif level == 2 then
					selectButton.ButtonText.Text = "Upgrade for " .. level3Cost
					selectButton.MouseButton1Click:Connect(function()
						game.ReplicatedStorage.PetSelectedRE:FireServer(true, pet)
					end)
				elseif level == 3 then
					selectButton.ButtonText.Text = "Max Upgrade"
					selectButton.MouseButton1Click:Connect(function() end)
				end
			else
				-- Handle case when CustomizePet is not found
			end
		else
			selectButton.MouseButton1Click:Connect(function()
				game.ReplicatedStorage.PetSelectedRE:FireServer(true, pet)
			end)
		end

		item.PetName.Text = pet.Name
		item.Parent = shopFrame.PetScroller

	end
end

updateShop()
updateInventory()

my script is not organized so I apologize in advance.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You need to introduce the concept of state into your code. There are many ways to do this, from table templates to metatables you just need to find some way to implement this thing called OOP for your shop items. Once that is done you are just storing the state of the objects and each player has their own different version of the state of all of the animals which gets saved to your datastore.

Hmm, I’m not getting any of what you just told me. I’m a beginner, and I apologize for that. I just know how to make a simple pet system. I want this to be done by me, but it seems so complicated. It looks like I need to find someone to teach me how to do this kind of stuff.

You just need to slow down and stop focusing on trying to make a pet system. Instead, you should study how data structures in lua work first and look at how other people coded pet systems and then come back to the problem

There are many ways to do this. Many complicated ways. A un-complicated way would be to just make a new pet. When they “upgrade” they really are just getting a new version of that pet.