How to use math.clamp() to limit cframe of models?

So, I have several models moving together and there is a problem of one of them are separating. Which means leaving the others behind because it is separated. I’ve been using math.clamp() to limit the cframe radius so they won’t separate. Afterall, none of my methods work.

The problem is, none of my code work. Instead it will just mess up the script.
I am not asking for entire code, just need my script to do the calculation the right way.
My Code:

local Radius = 2 -- Amount of studs for the radius
local Distance = (Model.PrimaryPart.Position - OtherModel.PrimaryPart.Position) -- No .Magnitude to be editable

local ModelBetweenCFrame = (Model.PrimaryPart.CFrame - Model.PrimaryPart.Position) + AnotherModel + Distance.Unit*Radius

The code above is my method of calculating the cframe between other models. In the code, Model is the first and OtherModel is same as model but referenced on other models and AnotherModel is the same as OtherModel, just the last model. I know the code is a bit messy and akward looking.

If you have any ideas, please reply me below and help me solve the problem. Because I am new at math operations in scripting like this, math.clamp()

You have any ideas or know any fixes or know some topics, please tell me and it would be greatly appreciated! :smile:

1 Like