local H={PetCostumeHat="\072\097\116",PetCostumeCollar="\067\111\108\108\097\114",PetCostumeBack="\067\097\112\101";PetCostumeAnkle="\065\110\107\108\101\116"}local u=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local z=require((u:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101"))local A=(require((u:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\083\101\114\118\101\114\083\099\114\105\112\116\083\101\114\118\105\099\101"))):GetContext(script)local Z=z:GetResource("\071\097\109\101\068\097\116\097\046\073\116\101\109\046\073\116\101\109\115")local W=z:GetResource("\071\097\109\101\068\097\116\097\046\073\116\101\109\046\080\101\116\083\107\105\110\084\101\120\116\117\114\101\115")local s=(z:GetResource("\069\120\116\101\114\110\097\108\085\116\105\108\046\078\101\120\117\115\073\110\115\116\097\110\099\101\046\078\101\120\117\115\073\110\115\116\097\110\099\101")):Extend()s:SetClassName("\080\101\116\083\101\114\118\105\099\101")A:SetContextResource(s)local V=A:GetResource("\083\101\114\118\105\099\101\046\080\108\097\121\101\114\068\097\116\097\083\101\114\118\105\099\101")local U=A:GetResource("\083\101\114\118\105\099\101\046\073\110\118\101\110\116\111\114\121\083\101\114\118\105\099\101")function s.SpawnPet(u,H)local A=H.Character if not A then return end local Z=A:FindFirstChild("\080\101\116")if Z then Z:Destroy()end local W=(z:GetResource("\080\101\116\077\111\100\101\108\115\046"..tostring((V:GetPlayerData(H)):GetValue("\067\117\114\114\101\110\116\080\101\116")))):Clone()local s=W:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")W.Name="\080\101\116"W.PrimaryPart=s W.Parent=A s:SetNetworkOwner(H)u:UpdatePet(H)end function s.UpdatePet(z,u)local A=u.Character if not A then return end local s=A:FindFirstChild("\080\101\116")if not s then return end local d=s:FindFirstChild("\067\108\111\116\104\101\115")if not d then return end local f=(V:GetPlayerData(u)):GetValue("\067\117\114\114\101\110\116\080\101\116")local v={}local B={}local R={}for H,z in pairs(H)do R[z]=H local A=U:GetItem(u,H)if A then v[H]=A.Name if v[H]then B[H]=(Z[A.Name]or{}).CostumeName end end end for H,u in pairs(d:GetChildren())do if u:IsA("\066\097\115\101\080\097\114\116")and R[u.Name]then local H=u:FindFirstChildWhichIsA("\070\105\108\101\077\101\115\104")if H then local z=v[R[u.Name]]if z then local A=Z[z]if A then u.Transparency=0 H.TextureId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..tostring(A.TextureId)end else u.Transparency=1 end end end end local g=B.PetCostumeHat==B.PetCostumeCollar and(B.PetCostumeHat==B.PetCostumeBack and B.PetCostumeHat==B.PetCostumeAnkle)local T=g and B.PetCostumeHat or"\068\101\102\097\117\108\116"local F=W[T][f]for H,u in pairs(s:GetChildren())do if u:IsA("\066\097\115\101\080\097\114\116")then local H=u:FindFirstChildWhichIsA("\070\105\108\101\077\101\115\104")if H then H.TextureId="\114\098\120\097\115\115\101\116\105\100\058\047\047"..tostring(F)elseif u:IsA("\077\101\115\104\080\097\114\116")then u.TextureID="\114\098\120\097\115\115\101\116\105\100\058\047\047"..tostring(F)end end end end return s
--[[
TheNexusAvenger

Controls spawning and updating pets.
--]]

local SLOT_TO_PART_NAME = {
	PetCostumeHat = "Hat",
	PetCostumeCollar = "Collar",
	PetCostumeBack = "Cape",
	PetCostumeAnkle = "Anklet",
}



local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ReplicatedStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ReplicatedStorage"))
local ServerScriptServiceProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ServerScriptService")):GetContext(script)

local ItemData = ReplicatedStorageProject:GetResource("GameData.Item.Items")
local PetSkinTextures = ReplicatedStorageProject:GetResource("GameData.Item.PetSkinTextures")

local PetService = ReplicatedStorageProject:GetResource("ExternalUtil.NexusInstance.NexusInstance"):Extend()
PetService:SetClassName("PetService")
ServerScriptServiceProject:SetContextResource(PetService)

local PlayerDataService = ServerScriptServiceProject:GetResource("Service.PlayerDataService")
local InventoryService = ServerScriptServiceProject:GetResource("Service.InventoryService")



--[[
Spawns a pet for the player.
--]]
function PetService:SpawnPet(Player)
    --Return if the character doesn't exist.
    local Character = Player.Character
    if not Character then
        return
    end

    --Destroy the existing pet.
    local ExistingPet = Character:FindFirstChild("Pet")
    if ExistingPet then
        ExistingPet:Destroy()
    end

    --Create the pet.
    local PetModel = ReplicatedStorageProject:GetResource("PetModels."..tostring(PlayerDataService:GetPlayerData(Player):GetValue("CurrentPet"))):Clone()
    local HumanoidRootPart = PetModel:WaitForChild("HumanoidRootPart")
    PetModel.Name = "Pet"
    PetModel.PrimaryPart = HumanoidRootPart
    PetModel.Parent = Character
    HumanoidRootPart:SetNetworkOwner(Player)

    --Update the pet's appearance.
    self:UpdatePet(Player)
end

--[[
Updates the pet's appearance for a player.
--]]
function PetService:UpdatePet(Player)
    --Return if the character doesn't exist.
    local Character = Player.Character
    if not Character then
        return
    end

    --Get the pet and return if it doesn't exist.
    local Pet = Character:FindFirstChild("Pet")
    if not Pet then
        return
    end
    local PetClothes = Pet:FindFirstChild("Clothes")
    if not PetClothes then
        return
    end

    --Get the pet information.
    local PetType = PlayerDataService:GetPlayerData(Player):GetValue("CurrentPet")
    local ClothingItemNames = {}
    local ClothingItemCostumeName = {}
    local PartNameToSlot = {}
    for SlotName,PartName in pairs(SLOT_TO_PART_NAME) do
        PartNameToSlot[PartName] = SlotName
        local Item = InventoryService:GetItem(Player,SlotName)
        if Item then
            ClothingItemNames[SlotName] = Item.Name
            if ClothingItemNames[SlotName] then
                ClothingItemCostumeName[SlotName] = (ItemData[Item.Name] or {}).CostumeName
            end
        end
    end

    --Update the clothes.
    for _,Part in pairs(PetClothes:GetChildren()) do
        if Part:IsA("BasePart") and PartNameToSlot[Part.Name] then
            local Mesh = Part:FindFirstChildWhichIsA("FileMesh")
            if Mesh then
                local ClothingItem = ClothingItemNames[PartNameToSlot[Part.Name]]
                if ClothingItem then
                    local ClothingItemData = ItemData[ClothingItem]
                    if ClothingItemData then
                        Part.Transparency = 0
                        Mesh.TextureId = "rbxassetid://"..tostring(ClothingItemData.TextureId)
                    end
                else
                    Part.Transparency = 1
                end
            end
        end
    end

    --Update the pet texture if all 4 parts are part of costume match.
	local HasFullCostume = (ClothingItemCostumeName["PetCostumeHat"] == ClothingItemCostumeName["PetCostumeCollar"]) and (ClothingItemCostumeName["PetCostumeHat"] == ClothingItemCostumeName["PetCostumeBack"]) and (ClothingItemCostumeName["PetCostumeHat"] == ClothingItemCostumeName["PetCostumeAnkle"])
	local Costume = HasFullCostume and ClothingItemCostumeName["PetCostumeHat"] or "Default"
    local PetTextureId = PetSkinTextures[Costume][PetType]
	for _,Part in pairs(Pet:GetChildren()) do
		if Part:IsA("BasePart") then
			local Mesh = Part:FindFirstChildWhichIsA("FileMesh")
			if Mesh then
                Mesh.TextureId = "rbxassetid://"..tostring(PetTextureId)
            elseif Part:IsA("MeshPart") then
                Part.TextureID = "rbxassetid://"..tostring(PetTextureId)
			end
		end
	end
end


return PetService