How to make an npc stay a certain amount of distance from the player

i have tried this

``````local PlayerToNpcDistanceX = (HRP.HumanoidRootPart.Position.X - NPCHRP.Position.X)
local PlayerToNpcDistanceZ = (HRP.HumanoidRootPart.Position.Z - NPCHRP.Position.Z)
local PosX
local PosZ

if PlayerToNpcDistanceX > 0 then
PosX =  Vector3.new(HRP.HumanoidRootPart.Position.X + 5)
end

if PlayerToNpcDistanceZ > 0 then
PosZ = Vector3.new(HRP.HumanoidRootPart.Position.Z + 5)
end

if PlayerToNpcDistanceX < 0 then
PosX = Vector3.new(HRP.HumanoidRootPart.Position.X - 5)
end

if PlayerToNpcDistanceZ < 0 then
PosZ = Vector3.new(HRP.HumanoidRootPart.Position.Z - 5)
end

--local PlayerToNpcDistanceZ = (HRP.HumanoidRootPart.Position.z - NPCHRP.Position.Z).Unit * 5
print(PosX)
print(PosZ)

local MoveTo = Vector3.new(PosX, NPCHRP.Position.Y, PosZ)

Humanoid:MoveTo(MoveTo)

print("LEFT")
``````

didnt work any ideas/help u can give

``````local StudsAway = 5

local NPC = workspace.Rig
local NPCRoot = NPC.HumanoidRootPart

local GetNearestPlayer = function()
local Nearest, NearestDist = nil, 9e9
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
if (v.Character) and (v.Character:FindFirstChild("Humanoid")) then
local Mag = (v.Character.HumanoidRootPart.Position - NPCRoot.Position).Magnitude

if (Mag < StudsAway) and (Mag < NearestDist) then
NearestDist = Mag
Nearest = v
end
end
end
return Nearest
end