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

How can I make an NPC roam freely?

Hey there, I've decided to begin making a game involving animal NPCs- currently, they don't need to be interactive, but I'd like them to roam around a biome/exhibit on their own. Looking it up, I could only find tutorials on how to make NPCs follow certain paths or travel from point A to point B. I'm sure I could figure out a way for this to work using paths, etc, but I don't particularly want the animals to be predictable and easy for players to locate.

As a summary- I need various NPC animals to wander around randomly, does anybody know a way I could do this?

Thanks for any help!

-Luminossy

3 comments

0

Try MoveTo() and math.random.

0

you need 2 cordinates for the moveto command

0

Learn about R6 and R15 characters, Pathfinding Serice, and raycasting.

2 answers

MoveTo() and math.random isnt a bad idea, but what if a build is blocking it? You'll have to use PathFindingService. You can get this service by doing game:GetService("PathFindingService")

You can make a table of Vector3 values like so:

local Spawns = {
    Vector3.new(0,10,0)
    Vector3.new(10,5,20)
}

and select a random value

NPC:MoveTo(Spawns[math.random(1,#Spawns)])

This can assure you that your NPC won’t walk into buildings and such, you can incorporate this into Pathfinding Service though.

An alternative would be selecting 3 random values;

local Ran1 = math.random(1,100)
local Ran2 = math.random(1,10)
local Ran3 = math.random(1,100)

And then moving the NPC;


npc:MoveTo(Vector3.new(Ran1,Ran2,Ran3)

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