Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

NPC MoveTo Player. How do I make the NPC keep at a certain distance?

Asked by 6 years ago
Edited 6 years ago

im using a script to make an NPC follow the player. Im trying to make the NPC follow the player, but stay at a certain distance from the player. like this...

https://imgur.com/a/fXwOUdL

if the player walks away, the NPC gets closer, if the player gets closer, the NPC moves farther away...to maintain the same distance from the player

01local Mroot = script.Parent.HumanoidRootPart
02local h = script.Parent.Humanoid
03local Mspawn = script.Parent.Parent.MobSpawn.CFrame
04 
05--Finds a player that is in range
06--use "local player = findplayer()"
07function findplayer()
08    for _,v in next, game.Players:GetPlayers()do
09        if v.Character then
10            local char = v.Character
11            if char:FindFirstChild("Humanoid")and char:FindFirstChild("HumanoidRootPart")then
12                local pRoot = char.HumanoidRootPart
13                if (pRoot.Position - Mroot.Position).magnitude <= 40 then
14                    return v
15                end
View all 30 lines...

ive tried doing this...

1while wait() do
2    local player = findplayer()
3    if player ~= nil then
4        local stayat = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-20)
5        h:MoveTo(stayat.p)
6    else
7        h:MoveTo(Mspawn.p)
8    end
9end

The NPC keeps the distance away from the player, but it tries to stay directly in front of the player... like this...

https://imgur.com/a/GY57ohj

https://imgur.com/a/Iwk9mq2

how do i make the NPC walk straight at the player, but stop at a certain distance...and if the player gets closer, walk away from the player, and remain the distance

0
Blank images TiredMelon 405 — 6y
0
ok fixed CaptnCosmic 54 — 6y

Answer this question