How Would I be able to Make an NPC Move near a player but not directly towards them

So what I want to achieve is for the NPC to move, say 10 studs near the player. I don’t want the NPC to move directly towards them because it’s a ranged npc, but I don’t want them too far. I want the NPC to move to the “goldilocks” zone, so to speak. I’ve been asking questions for three hours now without any help on other developer platforms, so I’ve come here alas to ask how to do this.
Here’s my script

while wait() do
	local e = game.Players:GetChildren()
	for i, v in pairs(e) do
		if v.Character then
			if v.Character:FindFirstChild("HumanoidRootPart") then
				if (v.Character.HumanoidRootPart.Position - script.Parent.RightFoot.Position).Magnitude<200 then
  					repeat wait(.5)
						script.Parent.Humanoid:LoadAnimation(script.Animation):Play()
						local ray = Ray.new(script.Parent["DC-15A"].crang.Position, (v.Character.HumanoidRootPart.Position - script.Parent["DC-15A"].crang.Position).unit * 80)
						local beam = Instance.new("Part",game.Workspace)
						local distance = (script.Parent["DC-15A"].crang.Position - v.Character.HumanoidRootPart.Position).magnitude
						beam.Anchored = true
						beam.Size = Vector3.new(0.2, 0.2, distance)
	    				beam.CFrame = CFrame.new(script.Parent["DC-15A"].crang.Position, v.Character.HumanoidRootPart.Position) * CFrame.new(0, 0, -distance / 2)
						beam.Transparency = 1
						beam.CanCollide = false
						game:GetService("Debris"):AddItem(beam,.01)
						
						local jar = Instance.new("Part",game.Workspace)
						local distance = (script.Parent["DC-15A"].crang.Position - v.Character.HumanoidRootPart.Position).magnitude
						jar.Position = script.Parent["DC-15A"].crang.Position
						jar.Size = Vector3.new(.1,.1,1)
						jar.Anchored = true
						jar.Material = "Neon"
						jar.CanCollide = false
						jar.BrickColor = BrickColor.new("Really red")
						jar.Orientation = beam.Orientation
						script.Parent.HumanoidRootPart["E-11 Blaster Sound"]:Play()
						
						game:GetService("Debris"):AddItem(jar,.5)
						
						for i = 1,50 do
							jar.CFrame = jar.CFrame + jar.CFrame.lookVector * 10
							wait(0.01)
						end
						script.Parent.Humanoid:MoveTo(v.Character.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position)
	 				until v.Character:WaitForChild("Humanoid").Health<=0
					
					v.Character.Humanoid.Health = v.Character.Humanoid.Health - 15
					v.Character.UpperTorso.Anchored = true
					local aga = v.Character:GetChildren()
					for i, v in pairs(aga) do
						wait()
						if v:IsA("Part") or v:IsA("MeshPart") then
							v.BrickColor = BrickColor.new("Light blue")
						end
					end
					
					wait(2.1)
					v.Character.UpperTorso.Anchored = false
					wait(3)
				end
			end
		end
		wait()
	end
end
function shoot(v)
	
end
9 Likes

What is this script doing (if anything at all), this way as opposed to dissecting your script I know what to look for.

1 Like

You could try doing something like this:

local dist = (v.Character.HumanoidRootPart.Position - script.Parent.RightFoot.Position).Magnitude 
local maxRange = 200
local minRange = 10
if dist > maxRange then
    --move NPC towards target
elseif dist < maxRange and dist > minRange then
    --stop NPC, attack target
elseif dist < minRange then
    --make NPC back up from target
end
2 Likes

How would I make it back away from the target? This is making it even more complex because in order to back up from the target the NPC would have to travel backwards from their spot relative to the player’s position, and in order to travel forwards, the same thing is applied but they travel towards the player instead of backwards, which in this case the NPC would just run around back and forth and it would look weird.

1 Like

All I want it to do is the NPC to move to the player, but in a sort of ring so that it doesn’t go inside of the player

1 Like

2 Likes
  1. Do you want the NPC to walk to a specific stud count away, or a general zone? (E.g. 10 studs away from player OR between 6-12 studs randomized each time)
  2. Do you want the NPC to circle the Player? Or just get within a certain range and then stop to attack, only continuing to move if they are further than the stud range?
  3. If the player walks towards the NPC, should the NPC try to maintain it’s distance? Or just keep attacking?

If you answer these questions we can help you with much more ease. It is somewhat hard to tell exactly what you intend to happen based on the information provided so far.

1 Like

To make the NPC walk in a circle around the target while still maintaining a distance from the it, I think you could cast a vector from the target at a random length between minRange and maxRange that’s a random number of degrees from the lookVector (somewhere below 90 degrees would probably be best), and then make the NPC walk to the endpoint.

Kinda like this:
Ring

4 Likes

I want the NPC to walk a specific stud count away, preferably 10 to 15 studs, or it could be easily randomized. I don’t want the NPC to circle the player, and the NPC will only move near the player when the player is close to the NPC. Again, the NPC is a ranged attacker, and only attacks when in range. The NPC can be easily damaged if the NPC moves directly to the player, hence why I am trying to make the NPC move to the player but a certain distance away, closest to where the NPC is. Say for example the NPC is to the right of the player. The NPC will move 10-15 studs to the player’s right, if the NPC is to the left of the player, the NPC will move 10-15 studs to the player’s left, same applies to all and any directions.

1 Like

You can find the target position the NPC will walk to by getting the distance from the player between the NPC and then subtracting it from your specific stud count. For example:

demo

Some simple maths would give you the target position, then you can use your desired method to move the NPC to that point, and attack the player
Hope this works…

2 Likes

To accomplish what you are looking for, I would utilize CFrame.LookVector. Here is an example of how you could incorporate it into your code:

local npc = script.Parent
local target = v.Character.HumanoidRootPart.Position
local maxDistance = 20

npc.Humanoid:MoveTo(target - CFrame.new(npc.HumanoidRootPart.Position, target).LookVector * maxDistance)

This would make the NPC move towards the target while remaining atleast 20 studs away from them.

27 Likes