Stop Cframe add an End point

I have this moving Cannon that moving down a track. The moving part using Cframe to move around. When you press “J” it moves to the left and when you press “K” it moving to the Right. But the track has an end point. How do I make it so the Moving part doesn’t keep moving once it reaches the end part.

1 Like

Have a look at the math.clamp() function.

How can I get that to interact with the Cframe? What would be the min and max values in that case?

Your min and max values will be the two endpoints (x,y, or z value) of the track.

Now is there a problem since I want to have multiple tracks with different cannons on them. Is there a way to do it so I don’t need to change the variables every time?

Any particular reason you want to CFrame it, as opposed to using a prismatic constraint and one of the velocity movers (so that it’s smooth in a live game).

If you do have to CFrame it, you can just clamp it by lerping between the endpoints ( leftEndCFrame:Lerp(rightEndCFrame, t), where 0 <= t <= 1 forces it to be clamped.

Sorry but I do not understand. Also I am using Cframe because I mostly use it. I do not really know hoe to use velocity too well

I have tried to use

if script.Parent.LeftBrick.Position == script.Parent.Parent.StopRailLeft then
print(‘stopleft’)
script.MoveLeft.Disabled = true
end

but it doesn’t seem to work