Efficient "Wander" function for NPCs?

Currently, my Wander function looks something like this:

function WanderModule.Wander(Wanderer)
	local newpos
	local WandererHum = Wanderer:WaitForChild("Humanoid")
	local HeadLoc = Wanderer.Head
	local x = math.random(-100,100) -- first corner of the boundary the NPC is allowed to roam
	local z = math.random(-100,100) -- the opposite corner of the boundary the NPC is allowed to roam
	newpos = Vector3.new(x, HeadLoc, z)
	WandererHum:MoveTo(newpos)
	WandererHum.MoveToFinished:Connect(print("Moved to", newpos))
end

However, my game’s map will definitely not be a quadrilateral shape and there definitely will be walls in the way. So how would I approach this?

You should use pathfinding, or, have a set path for your NPC to follow.

Also, for your print() is not being called correctly, you’re printing “Move to” straight away, and then you’re trying to run what print() returns, (nil) after its finished. You will get an error. You cannot call a function with brackets in a connect as it will call the function right away and run what it returns.