Quick Fix to NPC System

So I basically am making an NPC system for my game and want the NPC to go to a way point and then go back to its original spot to then destroy. Here is my code:

local hum = script.Parent:FindFirstChild("Humanoid")
local waypoints = workspace.Waypoints
local BackHome = workspace.Waypoints.BackHome
local roaming = true
local function Customer()
	local random = math.random(1, 5)
	hum:MoveTo(waypoints[random].Position)
	wait(10)
	hum:MoveTo(waypoints.BackHome.Position)
end

while roaming == true do
	wait()
	Customer()
end

The NPC Is going to a waypoint but then continuing going to other waypoints instead of going home. Unsure what to do.

2 Likes

Hey! Because you’re not giving the NPC time to travel back to the “Back Home” waypoint, after it starts heading there the loop instantly starts over. To prevent this, simply add another wait after calling the NPC to return home. An example is provided below.

Hope this helps!

local waypoints = workspace.Waypoints
local BackHome = workspace.Waypoints.BackHome
local roaming = true
local function Customer()
	local random = math.random(1, 5)
	hum:MoveTo(waypoints[random].Position)
	wait(10)
	hum:MoveTo(waypoints.BackHome.Position)
	wait(10)
end

while roaming == true do
	wait()
	Customer()
end

Thanks! I just realized that I need to add path finding to this some how. Thanks for the help though

Applied this to my pathfinding scripts, works great! Thanks

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