---------> Please Keep in mind that this is my first Thread <-------------
So What i am trying to create is a Smart NPC using PathFindingService
What i’m trying to do is make the path Reset Whenever the Part.Position Changes, And also for the npc to ignore the OLD generated path that doesn’t lead to the part ( because it already moved )
The Perfect example is Piggy Bot
So what i did that i tried to limit the AI movement to 2 steps ( 2 waypoints ) so it can compute and generate a new path
So here is my attempt:
--------------------- AI is in R6 -----------------------------
local PathFindingService = game:GetService("PathfindingService")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Torso = script.Parent:WaitForChild("Torso")
local EndingPoint = game.Workspace:WaitForChild("EndingPoint") --- this is just a part
local path = PathFindingService:CreatePath()
local WayPointsList = {}
local Limiter = 0 --- Number We gonna use to limit the path
wait(.5)
while true do ---- this is going to run every .2 Seconds
wait(.2)
path:ComputeAsync(Torso.Position,EndingPoint.Position) ---- Generating the path
local waypoints = path:GetWaypoints() --- Getting the way points
for i,Waypoint in pairs(waypoints) do ---- Looping through all the way points
if Limiter < 4 then ---- if the number is equal or less than 2 its gonna add a waypoint to the list
table.insert(WayPointsList,Waypoint) -- ading the waypoint
Limiter = Limiter + 1 --- adding one num so we can limit
print(Limiter)
else ---- if the number is bigger than 2 then it will break
break
end
end
for i,inList in pairs(WayPointsList) do ---- looping through the List
if Limiter == 4 then -- making sure the number is equal to 2
Humanoid:MoveTo(inList.Position) --- moving the npc to the listed waypoint
Limiter = 0 --- setting back the
table.remove(WayPointsList,1) --- removing it from the list
Humanoid.MoveToFinished:Wait() --- waiting till the AI has reached the point
else
end
end
end
So What am i trying to do here is limit the Path So can the AI PROPERLY Reach/Follow the Part ( if the path isn’t limited it wont follow properly because the part is moving ) by limiting it that means it resets the current position of the part every 2 waypoints that the npc moved to and then generates a new path and new waypoints, so it can keep on track of the moving part
Why did i add a limter you may ask? because it limits the added Waypoints to the table
RESULTS: The AI/NPC kept on circling around itself… … Well… that was a sad attempt and completly far from what i wanted
So uhhh that was my attempt, if you have a video/thread/Idea/Tuto that matches the goal Please reply and thanks
--Variables
local whilecountdown = 0
local pathservice = game:GetService("Pathfindingservice")
local path = pathservice:CreatePath()
local hum,hrp = script.Parent.Humanoid,script.Parent.HumanoidRootPart --2 variables at a single line
local EndingPoint = --case sensitive
--Path stuff
while wait() do
countdown = countdown + 1
path:ComputeAsync(hrp.Position,EndingPoint.Position)
for _,v in pairs(path:GetWaypoints()) do
hum:MoveTo(v)
hum.MoveToFinished:Wait()
end
if countdown == #path:GetWaypoints() then
break
end
end
I’ve Edited up a bit, But its the same as my old attempts ( not the one that is mentioned in the thread )
What i’m trying to do is make the path Reset Whenever the Part.Position Changes, And also for the npc to ignore the OLD generated path that doesn’t lead to the part ( because it already moved )
--Variables
local whilecountdown = 0
local pathservice = game:GetService("Pathfindingservice")
local path = pathservice:CreatePath()
local hum,hrp = script.Parent.Humanoid,script.Parent.HumanoidRootPart --2 variables at a single line
local EndingPoint = --case sensitive
--Path stuff
while wait() do
countdown = countdown + 1
path:ComputeAsync(hrp.Position,EndingPoint.Position)
EndingPart.PositionChanged:Connect(function() --when the part moves
local newPath = pathservice:CreatePath()
newPath:ComputeAsync(hrp.Position,EndingPart.Position)
for i,v in pairs(newPath:GetWaypoints()) do
hum:MoveTo(v.Position)
hum.MoveToFinished:Wait()
end
end)
for _,v in pairs(path:GetWaypoints()) do
hum:MoveTo(v)
hum.MoveToFinished:Wait()
end
if countdown == #path:GetWaypoints() then
break
end
end
It works When the Part.Position is changed, But there is a slight issue, There is a kind of corruption because the part is constantly moving , The AI Goes back and forth many times
Here is the game file for you to test E.rbxl (30.5 KB)
The script is placed in:
game > Workspace > NPC > PathFindingAI