Need help with a script

I need help, I tried to ask the assisant to code a script for my NPC where if its in about 50 stud proximity to the player it moves 150 studs away, but when I played the game the NPC stood still in place, I dont know if I need to make some remotes or something, because I just threw the script right in.

-- Script for making the model attempt to stay 150 studs away from the player

local model = script.Parent
local player = game.Players.LocalPlayer

local function distance(point1, point2)
    return (point1 - point2).Magnitude
end

while true do
    local playerPosition = player.Character.HumanoidRootPart.Position
    local modelPosition = model:GetModelCFrame().Position
    local currentDistance = distance(playerPosition, modelPosition)
    local targetDistance = 150
    local direction = (playerPosition - modelPosition).unit
    local moveVector = direction * (currentDistance - targetDistance)
    model:SetPrimaryPartCFrame(CFrame.new(modelPosition + moveVector))
    wait(0.1)
end

Try looking up and using the MoveTo() function, it might be what you are looking for.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.