I am writing a PathFinding script for an AI for learning purposes, but I’m facing an error
Here’s the script:
Does anyone know how to fix this error?
If I remove the {} from the Waypoints variable, the for loop gets ignored.
I am writing a PathFinding script for an AI for learning purposes, but I’m facing an error
Here’s the script:
Does anyone know how to fix this error?
If I remove the {} from the Waypoints variable, the for loop gets ignored.
Remove the {} from the Waypoints variable. To solve the loop getting ignored, first print the Waypoints variable to make sure it isn’t nil (if it is nil, that means there isn’t a suitable path to follow). Then, replace your for loop with
for i, waypoint in Waypoints do
--the rest of your code
end
Just to mention it, Roblox does provide a fully functioning PathFinding script on their Character Pathfinding documentation. Make sure to check that link out, and I have pasted the script below.
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local path = PathfindingService:CreatePath()
local player = Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local TEST_DESTINATION = Vector3.new(100, 0, 100)
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local function followPath(destination)
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(character.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
waypoints = path:GetWaypoints()
-- Detect if path becomes blocked
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
-- Check if the obstacle is further down the path
if blockedWaypointIndex >= nextWaypointIndex then
-- Stop detecting path blockage until path is re-computed
blockedConnection:Disconnect()
-- Call function to re-compute new path
followPath(destination)
end
end)
-- Detect when movement to next waypoint is complete
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
-- Increase waypoint index and move to next waypoint
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
-- Initially move to second waypoint (first waypoint is path start; skip it)
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("Path not computed!", errorMessage)
end
end
followPath(TEST_DESTINATION)
The loop gets ignored when I remove the {}.
Anyways, I tried my script in a simple course with more walking and minimal jumping and it fixed the issue, but when I add a course where the NPC should jump many times the code fails.
Anyway, thx for the help
Should it be
for i, waypoint in pairs(Waypoints) do
--the rest of your code
end
instead?
Also like @Whincify had said, remove the {} from the Waypoints variable
I did, and on a simple course and it worked, but on a more complicated one, the NPC gives up and chooses to go to the destination without going through the course and ignores the waypoints for loop
If I were to admit, the pathfinding on Roblox’s part still has some things that it can’t do. For example, a bot I made should be able to go through this gap in the first image displayed, yet sometimes it decides not to. In other cases, the bot decides that instead of jumping over a counter, it decides to walk around the whole way, taking longer. So, while the pathfinding is good, it still cannot perform some tasks, like climbing a ladder (I had to program that myself.)
In short term, the pathfinding bot is lazy and will go the easier route with less actions even if it is longer. I recommend putting walls so that the ai is forced to go through the course instead of going around.
Glad I helped you though!
I tried to and added invisible barriers, but the AI just walked into them instead of going through the course. And the course wasn’t too complicated, only jumping as if it was climbing a hill. The AI just ignores it and decides not to go there. I think if I wanted it to go through that course I would have to code the waypoints myself, which I can’t since I’m not that advanced in AI scripting.
I hope Roblox updates PathFinding and makes it less dumb and lazy
Could you send a video of this AI? I would like to see what it does exactly
Here is a video:
robloxapp-20220709-2243443.wmv (1.1 MB)
I don’t have any animation in the character of the NPC, so don’t mind that, but on such a course it ignored it completely while on a simpler one it doesn’t. It may be a simple issue as I’m still learning programming