Randomly Wandering NPC

Hello, I know the title sounds simple and that you can find it anywhere on the internet but I haven’t really found any method to this specific very simple request, which I find quite shocking.

I’m looking for an R6 NPC that wanders around the map aimlessly, like, with no math.random set destinations, no waypoint bricks, nothing fancy, just an NPC that wanders around the map, one that can even fall off the map if it has no borders.

I’ve seen a lot of these types of NPCs around games but personally I haven’t managed to make/find one exactly like them.

I have tried to script one but it just has that math.random preset destinations which just makes them walk to the random generated positions set in the brackets:

while task.wait(math.random(3,5)) do
	local x = math.random(-15, 15)
	local z = math.random(-15, 15)
	script.Parent:MoveTo(Vector3.new(x,script.Parent.Parent.Head,z))
end

If anyone has any idea on how to actually make one of them, please don’t be afraid to leave a reply.

1 Like

It is indeed very simple but I’m unsure wheter thats on the internet or not (im sure its hidden somewhere we both didn’t look):

you could try something like this:

----Variables:
local MapBounds = {Vector2.new(0,0),Vector2.new(100,100)} --x,y = x,z in 3d
local MaxHeight	= 100
----Functions:
local function getHeightAtPoint(position : Vector2)
	local Pos3D  = Vector3.new(position.X,MaxHeight,position.Y)
	local result = workspace:Raycast(Pos3D,Vector3.new(0,-1,0)*(MaxHeight*1.1),)
	
	if result and result.Position then
		return result.Position.Y
	end
end

local function getRandomPosition()
	local randX  = math.random(MapBounds[1].X,MapBounds[2].X)
	local randZ  = math.random(MapBounds[1].Y,MapBounds[2].Y)
	local height = getHeightAtPoint(Vector2.new(randX,randZ))
	
	if not height then error("no height found") end 
	
	return Vector3.new(randX,height+1,randZ)
end

while true do
	script.Parent:MoveTo(getRandomPosition())
	script.Parent.MoveToFinished:Wait()
end
3 Likes

Keep in mind that your little NPC also has to be able to jump and maybe look into pathfinding for better and (actually working) results! Character Pathfinding | Documentation - Roblox Creator Hub

1 Like
  1. I did look for an NPC like I wanted because the devforum wasn’t my first choice to ask for something like this because it seemed way too easy of a question to be asked on here.

  2. I tried your NPC script but they now are constantly on the move, I need them to just walk somewhere, stay there for a randomly generated amount of time and them be on the move again (my bad for not mentioning that in the topic).
    They also seem to be doing the same thing as before, they just go to a set range of an x and y positions and just wander around that zone, hence I was able to capture all 10 NPCs that were spawned in the same picture after letting them wander for 2-3 minutes:
    image_2024-01-14_022836486

  3. I don’t need the NPCs to jump, they should be fine walking around on a flat, not-so-deformed plain terrain.

Yet, thank you for trying to help me with this issue. If you have any updates, with the now mentioned details and issues that I provided in my reply, please let me know and I’ll try them out as well.

Well if you want them to stop, just add a task.wait(math.random(5,10)) or so after the script.Parent.MovedToFinished

1 Like

Well just set the range of x and z to ur map size (to make them move everywhere):

{Vector2.new(map.Position.X - map.Size.X/2, map.Position.Z - map.Size.Z/2),Vector2.new(map.Position.X + map.Size.X/2, map.Position.Z + map.Size.Z/2)}

Thought this was clear
Its 2 am and Im in bed I thus cant write code for u -sorry.

1 Like

Do you have any further questions or is it working to ur likings now?

1 Like

I am trying to make them spread out but with every change I make they seem to toward themselves in only one direction and I can’t figure out why.

Its as simple as them either following preset paths or them just walking to a random point along a zone. You can’t do this in any game without setting paths and or destinations randomly or preset. They need some direction

1 Like

Maybe send me footage and explain your problem - also: have u defines ur map as a part?

1 Like

I can’t send footage unfortunately but I’ll just use the preset math.random zones because I can’t really brainstorm how to find an actual fix to this. Thanks for the help though.

Wait wait wait, dont just give up!
Please try the new code I sent using the map. It should fix all ur problems. Local map = workspace.Baseplate

1 Like

Where should I place the line of code/coordinates ?

Replace map bounds with this:
{Vector2.new(map.Position.X - map.Size.X/2, map.Position.Z - map.Size.Z/2),Vector2.new(map.Position.X + map.Size.X/2, map.Position.Z + map.Size.Z/2)}

And remember to define map as ur baseplate or whatever part using:
Local map = workspace.Baseplate

Hope u got everything.
Good night.

1 Like

It worked, thank you very much.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.