How to limit model rotation so as not to exceed certain orientation

  1. What do you want to achieve?
    Preventing the model to turn less than 30 degrees and greater than 150 degrees at X

  2. What is the issue?
    The model keeps exceeding these two limits.

  3. What solutions have you tried so far?
    Tried using for loops with specific conditions: if currentXOrientation is less than target rotation, then decrease the degree/radians. if it is greater than increase the degree/radians.

Do keep in mind that this model is a child of another model with another primary part.

Edit: Forgot to add that the code below is another attempted solution of mines.

local function rotateMissilesHolder()
	
	local randomRotation = math.random(minRotationXMissilesHolder, maxRotationXMissilesHolder)

	local currentMissilesHolderOrientationX = math.floor(MissilesHolderPrimaryPart.Orientation.X)

	local rotationDifference = math.rad(randomRotation - currentMissilesHolderOrientationX)

	local sign = math.sign(rotationDifference)
	
	local rotationRateChangeWithDirectionRadians = math.rad(sign * rotationRateDegrees)

	local rotationChangeCFrame = CFrame.Angles(rotationRateChangeWithDirectionRadians, 0, 0)

	local newCFrame
	
	local currentRotationChange = 0

	repeat

		currentRotationChange += rotationRateChangeWithDirectionRadians

		newCFrame = MissilesHolderPrimaryPart.CFrame * rotationChangeCFrame

		MissilesHolder:SetPrimaryPartCFrame(newCFrame)

		wait(frameRate)

	until ((currentRotationChange - rotationDifference) >= 0)
	
end

The repeat until loop will not fire again once the until check has been completed. Try using part:GetPropertyChangedSignal("Orientation"), believing that the function isn’t inside another loop.

Hi!

You can use math.clamp(), to limit any numbers, including orientation.

You can’t listen for changes in the values of physics related properties via GetPropertyChangedSignal, you’ll need to use an event loop/regular loop instead.

Oh thanks!
Not sure how i had forgotten about this. I’m marking this as a solution.