How to make snake movement end in a straight position

I am currently using this module from this forum, on how to make snake-like movment:
Snake like movement

function Local.SolveIK(segments: table, startPart: BasePart, endPart: BasePart): nil
	local prevSegment: BasePart? = nil;
	for i = #segments, 1, -1 do
		local goalPosition: Vector3? = nil;
		local currentSegment: BasePart = segments[i];

		-- Set the goalPosition as the endPart if there was not a previous iteration
		if (not prevSegment) then
			goalPosition = endPart.Position
		else
			goalPosition = (prevSegment.CFrame * CFrame.new(0,0,prevSegment.Size.Z/2)).Position
		end

		local startPositionOfThisSegment: Vector3 = (currentSegment.CFrame * CFrame.new(0,0,currentSegment.Size.Z/2)).Position

		-- Set the CFrame from the goalPosition, facing the segment's current start position
		currentSegment.CFrame = (CFrame.new(goalPosition, startPositionOfThisSegment) * CFrame.new(0,0,-currentSegment.Size.Z/2)) * CFrame.Angles(0,math.pi,0)

		prevSegment = currentSegment
		
	end
end

However, the snake just stops moving and freezes at turns:

How do I make it so that even if the player stops moving, the snake will still go back to its original position of:

1 Like