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


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
