How to give player package based on leaderstat

So I have this script that gives a package based on your leaderstats. But it does not work in regular roblox, and never fully works the first time in studio, and not to the code is sloppy and way to many lines for it not to work. So any other ways I could make this work or just a different method (not humanoiddescription)

local OUTFIT_ENDPOINT = "rbxhttp://outfit-thumbnail/image?width=420&height=420&format=png&userOutfitId="

local AssetService = game:GetService("AssetService")
local InsertService = game:GetService("InsertService")

local bundleId = script:WaitForChild("BundleId")

local locked = true
local bundle = Instance.new("Folder")
local Players = game:GetService("Players")

local function tryGet(context, object, funcName, ...)
	local success, result = pcall(object, funcName, ...)
	
	if success then
		return result
	else
		warn("Invalid " .. context .. ":", ..., "error:", result)
	end
end

local function setLocked(state)
	locked = state
end

local function updateBundleId()
	bundle:ClearAllChildren()
	setLocked(true)
	
	local id = bundleId.Value
	local info = tryGet("BundleId", AssetService, "GetBundleDetailsAsync", id)
	
	if info then
		for _,item in pairs(info.Items) do
			if item.Type == "Asset" then
				local import = tryGet("Asset", InsertService, "LoadAsset", item.Id)
				if import then
					for _,child in pairs(import:GetChildren()) do
						child:Clone().Parent = bundle
					end
				end
			end
		end
		
		setLocked(false)
	end
end

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		if player:WaitForChild("leaderstats"):WaitForChild("Gender").Value == 2 and character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			local lastAppliedBundle = humanoid:FindFirstChild("LastAppliedBundle")
			if not lastAppliedBundle then
				lastAppliedBundle = Instance.new("IntValue")
				lastAppliedBundle.Name = "LastAppliedBundle"
				lastAppliedBundle.Parent = humanoid
			end
			
			if lastAppliedBundle.Value ~= bundleId.Value then
				local charMeshes = {}
				
				local oldAccessories = {}
				local newAccessories = {}
				
				setLocked(true)
				
				for _,accessory in pairs(humanoid:GetAccessories()) do
					accessory.Parent = nil
					table.insert(oldAccessories, accessory)
				end
				
				for _,child in pairs(character:GetChildren()) do
					if child:IsA("CharacterMesh") then
						charMeshes[child.BodyPart] = child
					end
				end
				
				for _,component in pairs(bundle:GetChildren()) do
					if component:IsA("Accessory") then
						table.insert(newAccessories, component:Clone())
					elseif component:IsA("SpecialMesh") or component:IsA("Decal") then
						local head = character:FindFirstChild("Head")
						
						if head then
							local oldComp = head:FindFirstChildOfClass(component.ClassName)
							if oldComp then
								oldComp:Destroy()
							end
							
							local newComp = component:Clone()
							newComp.Parent = head
						end
					elseif humanoid.RigType.Name == "R15" then
						if component.Name == "R15ArtistIntent" then 
							for _,limb in pairs(component:GetChildren()) do
								humanoid:ReplaceBodyPartR15(limb.Name, limb:Clone())
							end
						elseif component.Name == "R15Anim" then
							local animate = character:FindFirstChild("Animate")
							if animate then
								for _,newAnim in pairs(component:GetChildren()) do
									local oldAnim = animate:FindFirstChild(newAnim.Name)
									if oldAnim then
										oldAnim:Destroy()
									end
								
									newAnim:Clone().Parent = animate
								end
							end
						end
					end
				end
				
				local accessories = (#newAccessories > 0 and newAccessories or oldAccessories)
				
				for _,accessory in pairs(accessories) do
					humanoid:AddAccessory(accessory)
				end
				
				if humanoid.RigType.Name == "R15" then
					humanoid:BuildRigFromAttachments()
				end
				
				lastAppliedBundle.Value = bundleId.Value
				setLocked(false)
			end
		end
		end
		end)
	end)

updateBundleId()

bundleId.Changed:Connect(updateBundleId)