Advice on a start of a pet inventory system

Provide an overview of:

  • What does the code do and what are you not satisfied with?
    im making a pet inventory system, i made the start of it but i dont know how to make it more effiecient coz now its just long and i dont really know how to continue from now

  • What potential improvements have yo u considered?
    first i did normal datastores, now i did profileService and changed the script a bit

  • How (specifically) do you want to improve the code?
    im looking to make this script more effiecient and i want to make it more easier to read and change things

this is a script that has some more things like leaderstats and codes

	local Players = game:GetService("Players")
	local cachedProfiles = {}
	local ProfileService = require(script.ProfileService)
local moneyLib = require(game.ReplicatedStorage.MoneyLib)

local RunService = game:GetService("RunService")

local serverstorage = game:GetService("ServerStorage")

local codes = {
	"release",
	"taps",
}
-- coins = 0
-- gems = 1
-- boosts = 2
local codesPrizesTypes = {
	0,
	0

}

local HTTPService = game:GetService("HttpService")

local codePrize = {
	1000,
	250
}
local ExpiredCodes = {
	"thisisexpired"
}

local PetNames = {}
for i, pet in ipairs(game.ReplicatedStorage.Pets:GetChildren()) do
	PetNames[i] = pet.Name
end

local saveStructure = {
	Taps = 0;
	Coins = 0;
	Gems = 0;
	Zones = 0;
}

local CodesDataSave = {}
for i,code in pairs(codes) do
	CodesDataSave[code] = false
end

local PetsDataSave = {}

local PlayerProfileStore = ProfileService.GetProfileStore("PlayerSaveData", saveStructure)
local CodesProfileStore = ProfileService.GetProfileStore("CodesData", CodesDataSave)
local PetsProfileStore = ProfileService.GetProfileStore("PetsDataStoreeee", PetsDataSave)

local function PlayerAdded(player)
	local profile = PlayerProfileStore:LoadProfileAsync("Player_" .. player.UserId, "ForceLoad")
	local CodesProfile = CodesProfileStore:LoadProfileAsync("Player_" .. player.UserId, "ForceLoad")
	local PetsProfile = PetsProfileStore:LoadProfileAsync("Player_" .. player.UserId, "ForceLoad")
	print(PetsProfile.Data)

	if profile ~= nil then
		profile:ListenToRelease(function()
			cachedProfiles[player] = nil
			player:Kick("Your profile has been loaded remotely. Please rejoin.")
		end)

		if player:IsDescendantOf(Players) then
			cachedProfiles[player] = profile
			cachedProfiles[player.Name.."CodesUsed"] = CodesProfile
			cachedProfiles[player.Name.."Pets"] = PetsProfile
		else
			profile:Release() --he left
		end
	else
		player:Kick("Unable to load saved data. Please rejoin.")
	end
	
	local function checkQuanity(PetType)
		local Quanity = 0
		for i, pet in pairs(PetsProfile.Data) do
			if pet["Type"] == PetType then
				Quanity += 1
			end
		end
		return Quanity
	end
	local function checkHowMuchEquipped(PetType)
		local HowMuch = 0
		for i, pet in pairs(PetsProfile.Data) do
			if pet["Type"] == PetType and pet["Equipped"] == true then
				HowMuch += 1
			end
		end
		return HowMuch
	end

	local leaderStats = Instance.new("Folder", player)
	leaderStats.Name = "leaderstats"

	local realStats = Instance.new("Folder", player)
	realStats.Name = "RealStats"

	local RealTaps = Instance.new("NumberValue")
	RealTaps.Parent = realStats
	RealTaps.Value = profile.Data.Taps
	RealTaps.Name = "Taps"

	local Taps = Instance.new("StringValue")
	Taps.Value = 0
	Taps.Parent = leaderStats
	Taps.Name = "Taps"

	local playerFolder = Instance.new("Folder")
	playerFolder.Name = player.Name
	playerFolder.Parent = serverstorage 

	local TapsDebounce = Instance.new("IntValue")
	TapsDebounce.Name = "Debounce"
	TapsDebounce.Value = 0
	TapsDebounce.Parent = playerFolder

	local codesFolder = Instance.new("Folder")
	codesFolder.Name = "CodesUsed"
	codesFolder.Parent = player

	local PetsFolder = Instance.new("Folder", player)
	PetsFolder.Name = "Pets"	

	for i,code in ipairs(codes) do
		local codeValue = Instance.new("BoolValue")	
		codeValue.Value = false
		codeValue.Name = code
		codeValue.Parent = codesFolder
	end
	
	for i,pet in ipairs(PetsFolder:GetChildren()) do
		pet:FindFirstChild("Equipped"):GetPropertyChangedSignal("Value"):Connect(function()
			if pet:FindFirstChild("Equipped").Value == true then
				print(pet)
				local workspacePet = game.ReplicatedStorage.Pets:FindFirstChild(pet.Name):Clone()	
				workspacePet.Parent = workspace
			end
		end)
	end
	Taps.Value = moneyLib.DealWithPoints(RealTaps.Value)
	
	for i, petName in ipairs(PetNames) do
		local Quanity = checkQuanity(petName)
		print(Quanity)
		local EquippedCheck = checkHowMuchEquipped(petName)
		for i = 1, Quanity do
			local clonedPet = game.ReplicatedStorage.Pets:FindFirstChild(petName):Clone()
			clonedPet.Parent = PetsFolder
			if EquippedCheck > 0 then
				clonedPet.Equipped.Value = true
				EquippedCheck -= 1
			end
		end
	end
	
	PetsFolder.ChildAdded:Connect(function(pet)
		local petID = HTTPService:GenerateGUID(false);
		pet:FindFirstChild("PetId").Value = petID
		PetsProfile.Data[petID] = {
			["Equipped"] = false,
			["Type"] = pet.Name,
		}
		pet:FindFirstChild("Equipped"):GetPropertyChangedSignal("Value"):Connect(function()
			print("Changed")
			if pet:FindFirstChild("Equipped").Value == true then
				print("Equipped")
				PetsProfile.Data[petID] = {
					["Equipped"] = true,
					["Type"] = pet.Name,
				}
			else
				print(pet:FindFirstChild("Equipped").Value)
				PetsProfile.Data[petID] = {
					["Equipped"] = false,
					["Type"] = pet.Name,
				}
			end
		end)
	end)
	
	PetsFolder.ChildRemoved:Connect(function(pet)
		table.Remove(PetsProfile.Data, pet:FindFirstChild("PetId"))
	end)
end

--Joined before event was made
for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(function()
		PlayerAdded(player)
	end)()
end

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player)
	local profile = cachedProfiles[player]
	if profile ~= nil then
		print(profile.Data)
		profile:Release()
	end
end)

return cachedProfiles

this is a module script with profileService

Dont use HTTPService there is a limit per minute or something like that