Mob walks around

I’m trying to make a mob that moves around in random directions. I keep trying to script it though I come up blank. Any ideas on how I would make this work?

1 Like

you can reference the humanoid inside the npc and call the Humanoid:MoveTo() function and pass in a Vector3 with random coordinates using math.random() function

[ Humanoid | Roblox Creator Documentation ]

^ just in case you want to know how it works

Oh ok ye thanks!

(I’m pretty eh at scripting just letting you know)

I wrote this out, I know its wrong its just do I need to add variables or…

local function MoveTo ()
	Vector3.mathRandom(100,100,100)
end

Is that right so far, and if so what do I add next to it…

To follow up on this, you can use the pathfinding service to have your mob navigate the map using the same ideas.

There’s actually a wealth of resources for learning how to make rudimentary pathfinding AI’s. My favorite is Y3llow Mustang. He doesn’t go line by line. Instead gives an intuitive look at the logic behind pathfinding AI.

1 Like

you can make the Vector3 a variable, and to create a Vector3, you do Vector3.new()

im going to make a example if you do not understand

local position = Vector3.new(math.random(50), 0, math.random(50))
-- create a Vector3 position (0 on the y axis because you cannot move up)

Humanoid:MoveTo(position)
-- move to the created position
1 Like

Ok cool I understand. Now how would i implement that into lets say a simple part. What do I add to make it work.

you want an npc to move to a part?

No I’m saying the part is the npc and is randomly moving around.

instead of using a MoveTo function you can maybe set the part.Position to the position variable

(I may be wrong on this, there could be a better way to do it)

local Hum = script.Parent:WaitForChild("Humanoid")
while wait(math.random(1,5)) do
   local Dir = Vector3.new(math.random(-1000,1000),math.random(-1000,1000),math.random(-1000,1000)).Unit
   Hum:MoveTo(Hum.RootPart.Position + (Dir).Unit*100)
end

Thanks for the script, though could you explain what it means? thanks.

local Hum = script.Parent:WaitForChild("Humanoid") --defines and finds the humanoid
while wait(math.random(1,5)) do --loops every 1-5 seconds
   local Dir = Vector3.new(math.random(-1000,1000),math.random(-1000,1000),math.random(-1000,1000)).Unit - gets a random direction to move in
   Hum:MoveTo(Hum.RootPart.Position + (Dir).Unit*100) --moves the humanoid using :MoveTo
   -- it moves the human by putting it into where it is + the direction
end

Thanks!