Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a NPC back off?

Im to make a NPC which holds guns and shoots, but it will stay at a distance. How do I take the torso (which the gun holder is gonna move to) position and subtract the position with -5,0,0?

(this is the script)

hue = script.Parent.Torso

while wait(1) do
	for i, v in pairs(workspace:GetChildren()) do
		if v:IsA("Model") then
			z=v:FindFirstChild("ishuman")
				if z then
				for i, b in pairs(z.Parent:GetChildren()) do
					if b:IsA("Model") then
						if b.Name == v.Name then
							z1 = b:FindFirstChild("Torso")
							if (z1.Position - hue.Position).magnitude <= 20 then
								script.Parent.Humanoid:MoveTo() -- the players torsos position with Z posotion subtracted by 5
							end
						end
					end
				end
			end
		end
	end
end

2 comments

3

Post your code in the NPC?

0

I have a script similar to this. basically, what mine does it stops the script when it's in a 5 stud radius. I achieved this by using the loop while pos > 5 do

1 answer

✓ Accepted answer

You questions isn't well written and I'm not sure if what I am going to answer will be correct, but I will give it a go.

If you want to move your NPC -5 in the X axis, then you would do this:

game.Workspace.NPC.PrimaryPart = game.Workspace.NPC.Torso -- Lets MoveTo() know what to move
game.Workspace.NPC:MoveTo(game.Workspace.NPC.Torso.Position - Vector3.new(-5,0,0)

game.Workspace.NPC.Torso.Position signifies the Position of Torso to be subtracted by the Vector3 Above uses the method :MoveTo. This moves models, and is found in the Wiki here. I suggest using :MoveTo instead of :SetPrimaryPartCFrame because :MoveTo will not force it inside of a part, but on top if one is in the way.

Of course, you didn't give us any scripts so this wouldn't exactly be what you would use continuously push the NPC back if the player is too close.

This discussion is preserved as it was. New answers, comments, and votes are closed.