So I’m just trying to use roblox pathfinding to get an npc to use a car to go to a part. However it tries to get in the car with failing to jump in the car and pushing it a few times, and proceeds to give up and walk all the way to the part. Here is the script:
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local path = PathfindingService:CreatePath({
Costs = {
Plastic = 999999999999999,
UseCar = 1,
["AgentCanJump"] = true
}
})
local character = workspace.AI_NPC
local humanoid = character.Humanoid
local TEST_DESTINATION = workspace.Destination.Position
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
-- Use boat if waypoint label is "UseBoat"; otherwise move to next waypoint
if waypoints[nextWaypointIndex].Label == "UseBoat" then
useBoat()
else
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
end
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
function useBoat()
local boat = workspace.BoatModel
humanoid.Seated:Connect(function()
-- Start boat moving if agent is seated
if humanoid.Sit then
task.wait(1)
boat.CylindricalConstraint.Velocity = 5
end
-- Detect constraint position in relation to island
local boatPositionConnection
boatPositionConnection = RunService.PostSimulation:Connect(function()
-- Stop boat when next to island
if boat.CylindricalConstraint.CurrentPosition >= 94 then
boatPositionConnection:Disconnect()
boat.CylindricalConstraint.Velocity = 0
task.wait(1)
-- Unseat agent and continue to destination
humanoid.Sit = false
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
end
end)
end)
end
followPath(TEST_DESTINATION)
Thanks for your time and for bearing with my issues.