What this script does
Run a simple sequential patrol for an NPC you own, with optional pauses and an explicit cleanup function.
- Best for
- Prototype guards, pets, workers, and tutorial NPC movement in your own place.
- Language
- Luau
- Version
- 1.0.0
- Validation
- Luau checked · Studio integration required
Official Luau 0.735 parsing and static analysis pass with Roblox host types stubbed. Test the actual Instance, service, or event integration in a separate Studio place before production use.
Included behavior
- Sequential waypoint movement
- Optional pause at each point
- Cancelable patrol task
Install in the stated runtime
- 1Create a ModuleScript named Patrol.
- 2Paste the complete source and require it from a server or test Script in your own experience.
- 3Supply anchored waypoint parts and a server-owned NPC Humanoid, then save and call the returned stop function during cleanup.
Minimal usage example
local Patrol = require(script.Parent.Patrol)
local stop = Patrol.start(guard.Humanoid, workspace.GuardRoute:GetChildren(), 0.5)
-- Call stop() before removing the guard.Example inputs and result
- Supply
- A Humanoid, ordered waypoints, and a pause duration.
- Expect
- start returns a stop function; call it when the guard or route is removed.
These notes explain the example above. Read the complete source for every exported function and edge case before integrating it.
Complete source
No hidden import, minified block, or shortened loader.
--!strict
-- MIT License. Server NPC waypoint patrol for an experience you own or edit.
local Patrol = {}
function Patrol.start(humanoid: Humanoid, waypoints: { BasePart }, pauseSeconds: number?)
assert(#waypoints > 0, "At least one waypoint is required")
local state = { stopped = false }
local pause = math.max(0, pauseSeconds or 0)
task.spawn(function()
local index = 1
while not state.stopped and humanoid.Health > 0 do
local waypoint = waypoints[index]
if waypoint and waypoint.Parent then
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
if pause > 0 then task.wait(pause) end
end
index = index % #waypoints + 1
end
end)
return function() state.stopped = true end
end
return table.freeze(Patrol)
2107289d06ccef97b56b338941acf3e082f59ec205f2adc64f8adf58906d8f93Outside this script’s scope
- Track or target live players automatically
- Control NPCs in another experience
Found a problem or a better approach?
Tell us what failed, what you expected, and whether the issue happened in the Luau CLI or Roblox Studio. Suggestions for clearer types, safer server checks, or a more useful API are welcome.