Roblox Developer Logo Get Started Learn Roblox API Reference Community


Lua error in Module:API at line 338: attempt to index local 'class' (a nil value).


In this example, an NPC travels between two points, following paths created by PathfindingService. When following the path, the NPC looks at the Action of the waypoints it is trying to reach. If it encounters a waypoint it cannot get to by walking, the NPC’s Humanoid will be instructed to jump.

This code can be seen in action here. The place is not copy locked.

-- Make variables for Roblox services
local PathfindingService = game:GetService("PathfindingService")
-- Make variables for the NPC and some of its components
local wanderBot = game.Workspace.WanderBot
local rootPart = wanderBot.HumanoidRootPart
local humanoid = wanderBot.Humanoid
-- Make variables for the places the NPC will walk to
local goalA = game.Workspace.GoalA.Position
local goalB = game.Workspace.GoalB.Position
-- Function that moves the NPC to the passed in point
local function moveToPoint(point)
	-- Find a path to the destination and get the waypoints for the path
	local path = PathfindingService:FindPathAsync(rootPart.Position, point)
	local waypoints = path:GetWaypoints()
	-- Loop through all of the waypoints in the path
	for currentPoint = 1, #waypoints do
		-- Get the next waypoint to move to and move the humanoid towards it
		local waypoint = waypoints[currentPoint]
		-- Check the action of the next waypoint. If it is a jump waypoint
		-- then make the NPC jump
		if waypoint.Action == Enum.PathWaypointAction.Jump then			humanoid.Jump = true
		-- Wait until the NPC has reached the next waypoint
		local reachedTarget = humanoid.MoveToFinished:Wait()
-- Move the NPC between the two points in a loop
while true do