Can I make an NPC move wherever its wants when ever is wants?

As me and my team are apporaching close to the Beta realease of our game one thing we wanted to add was moving NPC.

I have creating a few NPC that are ready to code.

But I have 1 question can I make a NPC move where it wants.

Like tell him (In code). NPC can you stay within x and y and move when ever you please where ever you please in that area?

If you could give an example code that would be nice too.

Answer to questions DevForum ask you to Answer :smiley:

The problem is me and my team cant code NPC.

We are trying to make moving NPC.

We have not tried anything because we don’t know where to start.

1 Like

Use pathfinding, you can search it up. Character Pathfinding | Roblox Creator Documentation or on youtube or Character Pathfinding | Roblox Creator Documentation

Then repeat this using a While true do loop so it keep moving?

Yeah you can with both I believe.

-- Set patrol delay at 2 seconds
local PATROL_DELAY = 2
 
-- Variables for the zombie and its humanoid
local zombie = game.Workspace.Zombie
local humanoid = zombie.Humanoid
 
-- Variables for the point(s) the zombie should move between
local pointA = game.Workspace.GreenFlag
local pointB = game.Workspace.PurpleFlag
 
-- Variable to keep track of the zombie's next destination
local nextDestinationObject = pointA
 
-- Loop to move between the two points
while wait(PATROL_DELAY) do
	-- Move the zombie to the next destination
	humanoid:MoveTo(nextDestinationObject.PrimaryPart.Position) -- REMOVE PRIMARY PART IF IT ISN'T A MODEL!!!!!!!!!!
 
	-- Wait until the zombie has reached its target
	humanoid.MoveToFinished:Wait()
 
	-- Switch the current target to the other target
	if nextDestinationObject == pointA then
		nextDestinationObject = pointB
	else
		nextDestinationObject = pointA
	end
end

that will loop between 2 points that you set (taken from wiki). also remove primary part if it is a model

Also, pathfinding is if there is a block in the path to the destion. Such as a wall. Pathfinding tells the ai to moves around it.

You can use math.random. As long as you know the center point of the area and how far they can go from that point, you can just find a random point and move them there. You could also make them surround a player, inside the while loop just set center to the player’s humanoidrootpart’s position.

local center = Vector3.new(0,0,0)
local distance = 20

local npc = --define here

while wait(1) do
      local x, y = math.random(1,distance), math.random(1,distance)
      local newPos = center + Vector3.new(x, 0, y)
      npc.Humanoid:MoveTo(newPos)
     npc.Humanoid.MoveToFinshed:Wait()
end
1 Like

You could probably surround it with four parts then make them transparent and set can collide to false. You would have the character moving script in another script or you could use this one. In a script, you could put

game.Workspace.Part.Touched:Connect(function(hit) -- Change the name of the part to what you called it
if hit.Parent.Name = "NPC" then -- Change to your NPC's name
-- Just to be clear, the part below WON'T work so you'll need to get someone else for this bit.
hit.Humanoid.Rotation = Vector3.new(hit.Humanoid.Rotation + (0,180,0))
end
end)