-- theres gotta be a better way
local function getHumanoidDescriptionFromCharacterAppearance(characterAppearanceInfo)
	local humanoidDescription = Instance.new("HumanoidDescription")
	humanoidDescription.BodyTypeScale = characterAppearanceInfo.scales.bodyType
	humanoidDescription.DepthScale = characterAppearanceInfo.scales.depth
	humanoidDescription.HeadScale = characterAppearanceInfo.scales.head
	humanoidDescription.HeightScale = characterAppearanceInfo.scales.height
	humanoidDescription.ProportionScale = characterAppearanceInfo.scales.proportion 
	humanoidDescription.WidthScale = characterAppearanceInfo.scales.width
	humanoidDescription.HeadColor = BrickColor.new(characterAppearanceInfo.bodyColors.headColorId).Color
	humanoidDescription.LeftArmColor = BrickColor.new(characterAppearanceInfo.bodyColors.leftArmColorId).Color
	humanoidDescription.LeftLegColor = BrickColor.new(characterAppearanceInfo.bodyColors.leftLegColorId).Color
	humanoidDescription.RightArmColor = BrickColor.new(characterAppearanceInfo.bodyColors.rightArmColorId).Color
	humanoidDescription.RightLegColor = BrickColor.new(characterAppearanceInfo.bodyColors.rightLegColorId).Color
	humanoidDescription.TorsoColor = BrickColor.new(characterAppearanceInfo.bodyColors.torsoColorId).Color
	for i, asset in ipairs(characterAppearanceInfo.assets) do
		if asset.assetType.name == "Hat" then
			humanoidDescription.HatAccessory = humanoidDescription.HatAccessory .. "," .. asset.id
		elseif asset.assetType.name == "BackAccessory" or asset.assetType.name == "Back Accessory" then
			humanoidDescription.BackAccessory = humanoidDescription.BackAccessory .. "," .. asset.id
		elseif asset.assetType.name == "FaceAccessory" or asset.assetType.name == "Face Accessory" then
			humanoidDescription.FaceAccessory = humanoidDescription.FaceAccessory .. "," .. asset.id
		elseif asset.assetType.name == "FrontAccessory" or asset.assetType.name == "Front Accessory" then
			humanoidDescription.FrontAccessory = humanoidDescription.FrontAccessory .. "," .. asset.id
		elseif asset.assetType.name == "HairAccessory" or asset.assetType.name == "Hair Accessory" then
			humanoidDescription.HairAccessory = humanoidDescription.HairAccessory .. "," .. asset.id
		elseif asset.assetType.name == "NeckAccessory" or asset.assetType.name == "Neck Accessory" then
			humanoidDescription.NeckAccessory = humanoidDescription.NeckAccessory .. "," .. asset.id
		elseif asset.assetType.name == "ShoulderAccessory" or asset.assetType.name == "Shoulder Accessory" then
			humanoidDescription.ShouldersAccessory = humanoidDescription.ShouldersAccessory .. "," .. asset.id
		elseif asset.assetType.name == "WaistAccessory" or asset.assetType.name == "Waist Accessory" then
			humanoidDescription.WaistAccessory = humanoidDescription.WaistAccessory .. "," .. asset.id
		elseif asset.assetType.name == "Face" then
			humanoidDescription.Face = asset.id
		elseif asset.assetType.name == "Shirt" then
			humanoidDescription.Shirt = asset.id
		elseif asset.assetType.name == "Pants" then
			humanoidDescription.Pants = asset.id
		elseif asset.assetType.name == "Head" then
			humanoidDescription.Head = asset.id
		elseif asset.assetType.name == "LeftArm" or asset.assetType.name == "Left Arm" then
			humanoidDescription.LeftArm = asset.id
		elseif asset.assetType.name == "LeftLeg" or asset.assetType.name == "Left Leg" then
			humanoidDescription.LeftLeg = asset.id
		elseif asset.assetType.name == "RightArm" or asset.assetType.name == "Right Arm" then
			humanoidDescription.RightArm = asset.id
		elseif asset.assetType.name == "RightLeg" or asset.assetType.name == "Right Leg" then
			humanoidDescription.RightLeg = asset.id
		elseif asset.assetType.name == "Torso" then
			humanoidDescription.Torso = asset.id
		elseif asset.assetType.name == "ClimbAnimation" or asset.assetType.name == "Climb Animation" then
			humanoidDescription.ClimbAnimation = asset.id
		elseif asset.assetType.name == "FallAnimation" or asset.assetType.name == "Fall Animation" then
			humanoidDescription.FallAnimation = asset.id
		elseif asset.assetType.name == "IdleAnimation" or asset.assetType.name == "Idle Animation" then
			humanoidDescription.IdleAnimation = asset.id
		elseif asset.assetType.name == "JumpAnimation" or asset.assetType.name == "Jump Animation" then
			humanoidDescription.JumpAnimation = asset.id
		elseif asset.assetType.name == "RunAnimation" or asset.assetType.name == "Run Animation" then
			humanoidDescription.RunAnimation = asset.id
		elseif asset.assetType.name == "SwimAnimation" or asset.assetType.name == "Swim Animation" then
			humanoidDescription.SwimAnimation = asset.id
		elseif asset.assetType.name == "WalkAnimation" or asset.assetType.name == "Walk Animation" then
			humanoidDescription.WalkAnimation = asset.id
		end
	end
	if humanoidDescription.Shirt == 0 then
		humanoidDescription.Shirt = 855777286
	end
	if humanoidDescription.Pants == 0 then
		humanoidDescription.Pants = 855782781
	end
	return humanoidDescription
end

local function playerAdded(player)
	local function characterAdded(character)
		local characterAppearanceInfo = playerWearingAssets[player]
		if characterAppearanceInfo then
			local humanoid = character:FindFirstChildWhichIsA("Humanoid") or character:WaitForChild("Humanoid")
			local humanoidDescription = getHumanoidDescriptionFromCharacterAppearance(characterAppearanceInfo)
			if not humanoid or not humanoid:IsDescendantOf(workspace) then
				humanoid.AncestryChanged:Wait()
			end
			if humanoid.Health > 0 and humanoidDescription then
				pcall(function()
					humanoid:ApplyDescription(humanoidDescription)
				end)
			end
		end
	end
	
	characterAdded(player.Character or player.CharacterAdded:Wait())
	
	local characterAppearanceInfo
	pcall(function()
		characterAppearanceInfo = Players:GetCharacterAppearanceInfoAsync(player.UserId)
	end)
	playerWearingAssets[player] = characterAppearanceInfo or {}
	
	player.CharacterAdded:Connect(characterAdded)
end

local function playerRemoved(player)
	if playerWearingAssets[player] then
		playerWearingAssets[player] = nil
	end
end

for i, enumItem in ipairs(Enum.AssetType:GetEnumItems()) do
	AssetTypeEnumLookup[enumItem.Value] = enumItem
end

for i, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(playerAdded)(player)
end

Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerRemoved)
remoteEvent.OnServerEvent:Connect(onServerEvent)