--FUNCTIONS


local function getJointBetween(part0, part1)
	for _, obj in pairs(part1:GetChildren()) do
		if obj:IsA("Motor6D") and obj.Part0 == part0 then
			return obj
		end
	end
	for _, obj in pairs(part0:GetChildren()) do
		if obj:IsA("Motor6D") and obj.Part1 == part1 then
			return obj
		end
	end
end

function UpdatePositions()
	
	local targetKF = keyframeList[KFNumber]
	
	local function RecursivelyUpdatePositions(parentPose, poseObject)
		if parentPose then
			local FoundParent = rig:FindFirstChild(parentPose.Name, true)
			local FoundPoseObject = rig:FindFirstChild(poseObject.Name, true)
			if FoundParent and FoundPoseObject then
				local joint = getJointBetween(FoundParent, FoundPoseObject)
				if joint then
					
					--A keyframe Joint was found, proceed with schedulation
					lastPositions[joint] = joint.Transform
					desiredPositions[joint] = poseObject.CFrame
					--
					
				end
			end
		end
		
		for _, subPose in pairs(poseObject:GetSubPoses()) do
			RecursivelyUpdatePositions(poseObject, subPose)
		end
		
	end
	
	timeNeeded = targetKF.Time
	
	for _, poseObj in pairs(targetKF:GetPoses()) do
		RecursivelyUpdatePositions(nil, poseObj)
	end
	
end

function ResetJoints()
	for i, v in pairs(basePositions) do
		i.Transform = CFrame.new()
	end
end