local function x(x,k)if CUSTOMIZATION.Hats:FindFirstChild(k)then local i=CUSTOMIZATION.Hats[k]:Clone()local L=i.PrimaryPart local E=x[L.Name]for x,k in pairs(i:GetChildren())do if k:IsA("\066\097\115\101\080\097\114\116")and k~=L then local x=L.CFrame:ToObjectSpace(k.CFrame)local i=Instance.new("\087\101\108\100")i.Part0=E i.Part1=k i.C0=x i.Parent=k k.Anchored=false k.CanCollide=false k.Massless=true end end L:Destroy()i.Parent=x.Attachments end end local function k(x,k)if CUSTOMIZATION.Outfits:FindFirstChild(k)then local i=CUSTOMIZATION.Outfits[k]:FindFirstChild("\083\104\105\114\116")if i then i=i:Clone()i.Parent=x end local L=CUSTOMIZATION.Outfits[k]:FindFirstChild("\080\097\110\116\115")if L then L=L:Clone()L.Parent=x end end end local function i(x,k)if CUSTOMIZATION.Faces:FindFirstChild(k)then local i=CUSTOMIZATION.Faces[k]:Clone()i.Parent=x.Head end end local function L(x,k)if CUSTOMIZATION.SkinColors:FindFirstChild(k)then for x,i in pairs(x:GetChildren())do if i:IsA("\066\097\115\101\080\097\114\116")then i.Color=CUSTOMIZATION.SkinColors[k].Value end end end end local function E(x,k)if CUSTOMIZATION.Backpacks:FindFirstChild(k)then local i=CUSTOMIZATION.Backpacks[k]:Clone()local L=i.PrimaryPart local E=x[L.Name]for x,k in pairs(i:GetChildren())do if k:IsA("\066\097\115\101\080\097\114\116")and k~=L then local x=L.CFrame:ToObjectSpace(k.CFrame)local i=Instance.new("\087\101\108\100")i.Part0=E i.Part1=k i.C0=x i.Parent=k k.Anchored=false k.CanCollide=false k.Massless=true end end L:Destroy()i.Parent=x.Attachments end end local function s(x,k)if CUSTOMIZATION.Emotes:FindFirstChild(k)then k=CUSTOMIZATION.Emotes[k]:Clone()k.Parent=x.Animate.Animations.Emotes end end local function y(y)if y then local B=PLAYER_DATA:WaitForChild(y.Name)x(y,B.Equipped.Hat.Value)x(y,B.Equipped.Hat2.Value)k(y,B.Equipped.Outfit.Value)i(y,B.Equipped.Face.Value)L(y,B.Equipped.SkinColor.Value)E(y,B.Equipped.Backpack.Value)s(y,B.Equipped.Emote.Value)end end local function B(x)x.CharacterAdded:connect(y)y(x.Character)end
-- functions

local function EquipHat(character, hat)
	if CUSTOMIZATION.Hats:FindFirstChild(hat) then
		local hat = CUSTOMIZATION.Hats[hat]:Clone()
		local attach = hat.PrimaryPart
		local charAttach = character[attach.Name]

		for _, v in pairs(hat:GetChildren()) do
			if v:IsA("BasePart") and v ~= attach then
				local offset = attach.CFrame:ToObjectSpace(v.CFrame)

				local weld = Instance.new("Weld")
				weld.Part0 = charAttach
				weld.Part1 = v
				weld.C0 = offset
				weld.Parent = v

				v.Anchored = false
				v.CanCollide = false
				v.Massless = true
			end
		end

		attach:Destroy()
		hat.Parent = character.Attachments
	end
end

local function EquipOutfit(character, outfit)
	if CUSTOMIZATION.Outfits:FindFirstChild(outfit) then
		local shirt = CUSTOMIZATION.Outfits[outfit]:FindFirstChild("Shirt")
		if shirt then
			shirt = shirt:Clone()
			shirt.Parent = character
		end
		local pants = CUSTOMIZATION.Outfits[outfit]:FindFirstChild("Pants")
		if pants then
			pants = pants:Clone()
			pants.Parent = character
		end
	end
end

local function EquipFace(character, face)
	if CUSTOMIZATION.Faces:FindFirstChild(face) then
		local face = CUSTOMIZATION.Faces[face]:Clone()
		face.Parent = character.Head
	end
end

local function EquipSkinColor(character, skinColor)
	if CUSTOMIZATION.SkinColors:FindFirstChild(skinColor) then
		for _, v in pairs(character:GetChildren()) do
			if v:IsA("BasePart") then
				v.Color = CUSTOMIZATION.SkinColors[skinColor].Value
			end
		end
	end
end

local function EquipBackpack(character, backpack)
	if CUSTOMIZATION.Backpacks:FindFirstChild(backpack) then
		local backpack = CUSTOMIZATION.Backpacks[backpack]:Clone()
		local attach = backpack.PrimaryPart
		local charAttach = character[attach.Name]

		for _, v in pairs(backpack:GetChildren()) do
			if v:IsA("BasePart") and v ~= attach then
				local offset = attach.CFrame:ToObjectSpace(v.CFrame)

				local weld = Instance.new("Weld")
				weld.Part0 = charAttach
				weld.Part1 = v
				weld.C0 = offset
				weld.Parent = v

				v.Anchored = false
				v.CanCollide = false
				v.Massless = true
			end
		end

		attach:Destroy()
		backpack.Parent = character.Attachments
	end
end

local function EquipEmote(character, emote)
	if CUSTOMIZATION.Emotes:FindFirstChild(emote) then
		emote = CUSTOMIZATION.Emotes[emote]:Clone()
		emote.Parent = character.Animate.Animations.Emotes
	end
end

local function HandleCharacter(character)
	if character then
		local playerData = PLAYER_DATA:WaitForChild(character.Name)

		EquipHat(character, playerData.Equipped.Hat.Value)
		EquipHat(character, playerData.Equipped.Hat2.Value)
		EquipOutfit(character, playerData.Equipped.Outfit.Value)
		EquipFace(character, playerData.Equipped.Face.Value)
		EquipSkinColor(character, playerData.Equipped.SkinColor.Value)
		EquipBackpack(character, playerData.Equipped.Backpack.Value)
		EquipEmote(character, playerData.Equipped.Emote.Value)
	end
end

local function HandlePlayer(player)
	player.CharacterAdded:connect(HandleCharacter)

	HandleCharacter(player.Character)
end
