Make Character move to the closest position that is certain distance away from a part

local part
humanoid:MoveTo(part.Position +,0,5))
			(player.Character.PrimaryPart.Position - part.Position).Magnitude <= 15

I am trying to make it so that it moves the character closer to the part so the distance between the player and the part can be 15 or less studs. But I am not sure how to make it so it calculates what is the closest position so that the position is 15 or less studs apart from the part

1 Like

Take the vector from the part towards the player.

If it’s less than 15 studs long, do nothing.

Otherwise, clip the length of that vector to fifteen studs and use that as your target position.

local vec = player.Character.PrimaryPart.Position - part.Position
local dist = vec.Magnitude
local max = 15
if dist > max then
  local unitVec = vec / dist
  humanoid:MoveTo(part.Position + unitVec * max)