Basically this ai script won’t work and doesn’t let the NPC move
Nothing is anchored or welded to anything so I’m not sure why it won’t move, I am very new to AI scripting since tutorials are just impossible to understand so I’m not surprised this ain’t working
local pathfindingService = game:GetService("PathfindingService")
local NPC = script.Parent
local humanoid = NPC.Humanoid
local HumanoidRootPart = NPC.HumanoidRootPart
local endpos = workspace.endpos
local NPCpath = pathfindingService:CreatePath()
NPCpath:ComputeAsync(HumanoidRootPart.Position,endpos.Position)
local wayPoints = NPCpath:GetWaypoints()
for _,wayPoint in pairs(wayPoints) do
humanoid:MoveTo(wayPoint.Position)
humanoid.MoveToFinished:Wait()
end
Works fine for me, just dropped the script into an empty dummy and it works.
Time for some debugging any errors?
If not then I will list some possible causes.
-
Path is blocked (most likely)
-
Is the script running?
-
Network Ownership (humanoid physics interference)
-
Humanoid hip height incorrect (it cannot move)
For path is blocked
There is also the dev hub.
Looking into it will give you example code which you will need to usually take into account for, for example:
https://developer.roblox.com/en-us/api-reference/function/Path/ComputeAsync
Basically tells you, that one will need to manage if the path is possible if it is blocked.
path.Blocked:Connect(OnPathBlocked)
-- When the path is blocked...
function OnPathBlocked(blockedWaypointIdx)
-- Check if the obstacle is further down the path
if blockedWaypointIdx > currentWaypointIdx then
-- Recompute the path
path:ComputeAsync(updatedHumanoidPos, targetPosition)
if path.Status == Enum.PathStatus.Succeess then
-- Retrieve update waypoint list with path:GetWaypoints()
-- and Continue walking towards target
else
-- Error, path not found
warn("Path not found!")
end
end
end
2 Likes
Thanks! I found out that for some reason despite being unachored, it was anchored? I simply resetted the anchor button and it now works, but thanks for the response since it made me go back to test it out! Plus, it made me learn a bit more.