WARNING: NOT THE PRETTIEST CODE IM JUST TRYING TO LEARN HOW TO USE THE PathfindingService
So the problem is with the MoveTo on the followPath function on line 37
local pathFindingService = game:GetService("PathfindingService"); local path = pathFindingService:CreatePath(); local NPC = script.Parent; local NPC_Humanoid = NPC.Humanoid; local waypoints; local currentWaypointIndex; local function findNearestPlayer(position) local lowest = math.huge; local nearestPlayer; for _, v in pairs(game.Players:GetPlayers()) do if v then if v.Character then local distance = v:DistanceFromCharacter(position); if distance <= lowest then lowest = distance; nearestPlayer = v; print(nearestPlayer.Name.." is closest player at a distance of :".. distance ); return nearestPlayer; end end end end end local function followPath(player) path:ComputeAsync(NPC.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position); waypoints = {}; if path.Status == Enum.PathStatus.Success then waypoints = path:GetWaypoints(); print("lets see if i did a thing"); currentWaypointIndex = 1; NPC_Humanoid:MoveTo(waypoints[currentWaypointIndex].Position); print("now it should be moving"); else NPC_Humanoid:MoveTo(NPC.HumanoidRootPart.Position); end end local function onWaypointReached(reached) if reached and currentWaypointIndex < #waypoints then currentWaypointIndex = currentWaypointIndex + 1; NPC_Humanoid:MoveTo(waypoints[currentWaypointIndex].Position); end end local function onPathBlocked(blockedWaypoint) if blockedWaypoint > currentWaypointIndex then local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position); followPath(nearestPlayer); end end while true do wait(15); local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position); if nearestPlayer then followPath(nearestPlayer); end end path.Blocked:Connect(onPathBlocked) NPC_Humanoid.MoveToFinished:Connect(onWaypointReached)
It does change the WalkToPoint but the NPC (Clone of my character from in game) does not actually walk to that point I can't figure out why
Here is the WalkToPoint before MoveTo was called
also sorry for hurting your eyes with this ugly code I just want to figure this out I have been stuck for almost a day now .-. It worked like twice then it just stopped working.
It's because you're connecting the events after the while loop. As the while loop doesn't let anything else after it run, the events will never connect.
Move the connects to before the while loop, and it should be fine.
path.Blocked:Connect(onPathBlocked) NPC_Humanoid.MoveToFinished:Connect(onWaypointReached) while true do wait(5); local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position); if nearestPlayer then followPath(nearestPlayer); end end
Hello,
local pathFindingService = game:GetService("PathfindingService"); local path = pathFindingService:CreatePath(); local NPC = script.Parent; local NPC_Humanoid = NPC.Humanoid; local waypoints; local currentWaypointIndex = 1; local function findNearestPlayer(position) local lowest = math.huge; local nearestPlayer; for _, v in pairs(game.Players:GetPlayers()) do if v then if v.Character then local distance = v:DistanceFromCharacter(position); if distance <= lowest then lowest = distance; nearestPlayer = v; print(nearestPlayer.Name.." is closest player at a distance of :".. distance ); return nearestPlayer; end end end end end local function followPath(player) path:ComputeAsync(NPC.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position); waypoints = {}; if path.Status == Enum.PathStatus.Success then waypoints = path:GetWaypoints(); -- currentWaypointIndex = 1; -- You are declaring the index being 1. print(currentWaypointIndex) NPC_Humanoid:MoveTo(waypoints[currentWaypointIndex].Position); else NPC_Humanoid:MoveTo(NPC.HumanoidRootPart.Position); end end local function onWaypointReached(reached) if reached and currentWaypointIndex < #waypoints then currentWaypointIndex = currentWaypointIndex + 1; NPC_Humanoid:MoveTo(waypoints[currentWaypointIndex].Position); end end local function onPathBlocked(blockedWaypoint) if blockedWaypoint > currentWaypointIndex then local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position); followPath(nearestPlayer); end end while true do wait(15); local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position); if nearestPlayer then followPath(nearestPlayer); end end path.Blocked:Connect(onPathBlocked) NPC_Humanoid.MoveToFinished:Connect(onWaypointReached)
So, having redacted the mistake, I'll proceed to explain.
In the while loop, you are checking for the nearestPlayer
and "following" it with the function
followPath
, for now everything was good, but on Line 24, you were declaring the currentWaypointIndex
to be 1, constantly as it is inside of a while loop.
To counter this, we make currentWaypointIndex
a "global" in the scope of this script; Line 7.
This should fix your issue, any further questions DM me.
PS:Please accept this as an answer if this helped you.