Hello. I am trying to use PathfindingService to create a path for an NPC to walk when a player touches a part. At line 45 of the code (the one that says for _, in pairs()) , the for loop doesn't run. I think that's the case since the print() statement at line 47 wasn't printed.
the script goes like so: when the player steps on "activate", the function(OnPartTouched) will run.
i've included which statements have been printed. this script is in starter character scripts and its a local script.
any idea why for in pairs() loop isn't running?
any help is appreciated!
my code is pasted below.
local PathFindingService = game:GetService("PathfindingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local T1NPC1 = game.Workspace.T1NPC["1"].T1NPC1
local NPCHumanoid = T1NPC1.Humanoid
local Activate = game.Workspace.T1NPC["1"].Activate
local path = PathFindingService:CreatePath()
local CanMakePath = true
local function OnPartTouched(hit)
if CanMakePath == true then
CanMakePath = false
print("Function fired")--printed
local LocalPlayer = game.Players.LocalPlayer
local LocalPlayerCharacter = LocalPlayer.Character
local IsBattled = LocalPlayer.NPCsBattled.T1["1"].IsBattled
if IsBattled.Value == false and LocalPlayerCharacter and LocalPlayerCharacter:FindFirstChildOfClass("Humanoid")then
local PlayerHumanoid = LocalPlayerCharacter.Humanoid
PlayerHumanoid.WalkSpeed = 0
local CreatePart = Instance.new("Part")
CreatePart.Position = LocalPlayerCharacter.HumanoidRootPart.Position - Vector3.new(0,2.5,0)
CreatePart.Name = LocalPlayer.Name.."'sAlertPart" --this part has been created
CreatePart.Transparency,CreatePart.Anchored,CreatePart.CanCollide = 1,true,false
CreatePart.Parent = workspace
print(LocalPlayer.Name.." has created a part due to encountering an NPC")--printed
warn(tostring(LocalPlayer.Name.." is gonna create a part."))--warned/printed
path:ComputeAsync(T1NPC1.HumanoidRootPart.Position,CreatePart.Position)
print("the NPC gonna find a way to da player")--printed
local waypoints = path:GetWaypoints()
print("NPC got waypoints")--printed
for _, waypoint in pairs(waypoints) do
print("NPC will move") --this doesn't print, which means this for loop didn't fire at all
local WP = Instance.new("Part")
WP.Shape = "Ball"
WP.Material = "Neon"
WP.Size = Vector3.new(0.6, 0.6, 0.6)
WP.Position = waypoint.Position
WP.Anchored = true
WP.CanCollide = false
WP.Parent = game.Workspace
NPCHumanoid:MoveTo(waypoint.Position)
NPCHumanoid.MoveToFinished:Wait()
end
end
end
end
Activate.Touched:Connect(OnPartTouched)
--this script is inside startercharacterscripts and its a Localscript
--there are no error messages

4 comments
i forgot to mention that there is no error in the output
do print(unpack(waypoints)) before the loop
i've added it in, and it didn't print
does that mean that the script/pathfindingservice couldn't get any waypoints?