NPCMover Script - local pathService = game:GetService("PathfindingService") local NPC = script.NPC.Value local humanoid = NPC.Humanoid local npcPaths = workspace:FindFirstChild("NPCPaths") local npcPoints = npcPaths:FindFirstChild("NPCPoints") local spawnpoint, endpoint -- Generate random spawn and end point repeat spawnpoint = npcPoints:GetChildren()[math.random(1,#npcPoints:GetChildren())] endpoint = npcPoints:GetChildren()[math.random(1,#npcPoints:GetChildren())]
task.wait() until spawnpoint ~= endpoint -- Move NPC to spawn NPC:PivotTo(spawnpoint.CFrame) -- Animations local idleAnim = Instance.new("Animation",humanoid) idleAnim.AnimationId = "rbxassetid://507766388" idleAnim = humanoid.Animator:LoadAnimation(idleAnim) local walkAnim = Instance.new("Animation",humanoid) walkAnim.AnimationId = "rbxassetid://507767714" walkAnim = humanoid.Animator:LoadAnimation(walkAnim) -- Roblox Documentation - PathfindingService local Waypoints, NextWaypoint, Reached, Blocked -- Determine what the NPC will and wont walk on local mCosts = { -- Prioritize Smaller Numbers Concrete = 1,
-- If the path succeeds if success and path.Status == Enum.PathStatus.Success then Waypoints = path:GetWaypoints() Blocked = path.Blocked:Connect(function(blockedWaypointIndex) if blockedWaypointIndex >= NextWaypoint then Blocked:Disconnect() moveNPC(endPoint) end end) if not Reached then -- If the NPC still needs to move Reached = humanoid.MoveToFinished:Connect(function(reached) if reached and NextWaypoint < #Waypoints then NextWaypoint += 1 humanoid:MoveTo(Waypoints[NextWaypoint].Position) if walkAnim.IsPlaying ~= true then idleAnim:Stop() walkAnim:Play() end else walkAnim:Stop() idleAnim:Play()
Reached:Disconnect() Blocked:Disconnect()
task.wait(1)
NPC:Destroy() script:Destroy() end end) end NextWaypoint = 2 humanoid:MoveTo(Waypoints[NextWaypoint].Position) else warn(errorMessage)
NPC:Destroy() script:Destroy() end end moveNPC(endpoint)
ServerScriptService.NPC Handler:139: attempt to compare nil = nextwaypoint then blocked:Disconnect() moveNPC(endpoint) end I also get a warn error message of nil
NPCMover Script -
local pathService = game:GetService("PathfindingService")
local NPC = script.NPC.Value
local humanoid = NPC.Humanoid
local npcPaths = workspace:FindFirstChild("NPCPaths")
local npcPoints = npcPaths:FindFirstChild("NPCPoints")
local spawnpoint, endpoint
-- Generate random spawn and end point
repeat
spawnpoint = npcPoints:GetChildren()[math.random(1,#npcPoints:GetChildren())]
endpoint = npcPoints:GetChildren()[math.random(1,#npcPoints:GetChildren())]
task.wait()
until spawnpoint ~= endpoint
-- Move NPC to spawn
NPC:PivotTo(spawnpoint.CFrame)
-- Animations
local idleAnim = Instance.new("Animation",humanoid)
idleAnim.AnimationId = "rbxassetid://507766388"
idleAnim = humanoid.Animator:LoadAnimation(idleAnim)
local walkAnim = Instance.new("Animation",humanoid)
walkAnim.AnimationId = "rbxassetid://507767714"
walkAnim = humanoid.Animator:LoadAnimation(walkAnim)
-- Roblox Documentation - PathfindingService
local Waypoints, NextWaypoint, Reached, Blocked
-- Determine what the NPC will and wont walk on
local mCosts = {
-- Prioritize Smaller Numbers
Concrete = 1,
Plastic = 1000,
Grass = 1000,
SmoothPlastic = 1000
}
-- Create path
local path = pathService:CreatePath({
AgentRadius = 3,
AgentHeight = 5,
AgentCanJump = false,
Costs = mCosts
})
local function moveNPC(endPoint)
local success, errorMessage = pcall(function()
path:ComputeAsync(NPC.HumanoidRootPart.Position, endPoint.Position)
end)
-- If the path succeeds
if success and path.Status == Enum.PathStatus.Success then
Waypoints = path:GetWaypoints()
Blocked = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= NextWaypoint then
Blocked:Disconnect()
moveNPC(endPoint)
end
end)
if not Reached then -- If the NPC still needs to move
Reached = humanoid.MoveToFinished:Connect(function(reached)
if reached and NextWaypoint < #Waypoints then
NextWaypoint += 1
humanoid:MoveTo(Waypoints[NextWaypoint].Position)
if walkAnim.IsPlaying ~= true then
idleAnim:Stop()
walkAnim:Play()
end
else
walkAnim:Stop()
idleAnim:Play()
Reached:Disconnect()
Blocked:Disconnect()
task.wait(1)
NPC:Destroy()
script:Destroy()
end
end)
end
NextWaypoint = 2
humanoid:MoveTo(Waypoints[NextWaypoint].Position)
else
warn(errorMessage)
NPC:Destroy()
script:Destroy()
end
end
moveNPC(endpoint)
great video man! keep going :D
ServerScriptService.NPC Handler:139: attempt to compare nil = nextwaypoint then
blocked:Disconnect()
moveNPC(endpoint)
end
I also get a warn error message of nil
Could you provide me with your entire moveNPC local function?
@@tropicalmasterpiece it was because the nextwaypoint variable was nil when trying to compare it
is there a way to pathfind on certain parts with names instead of materials?
I'm not sure, probably not
But you can also use invisible parts with a concrete material if you want the NPC to follow specific paths.
@@tropicalmasterpiece OHH I FOUND THE ISSUE. the parts were invisible, but need cancollide to properly work as a pathfinding material/part