the pathfinding script im currently using struggles to decide where to go past a waypoint. I have several way points and they are all accessible.
Script
local Demon = script.Parent
local humanoid = Demon.Humanoid
Demon.PrimaryPart:SetNetworkOwner(nil)
local PathfindingService = game:GetService(“PathfindingService”)
local function canSeeTarget(target)
local origin = Demon.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - Demon.HumanoidRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, Demon)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 60
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (Demon.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {
["AgentHeight"] = 8.5,
["AgentRadius"] = 11,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(Demon.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (Demon.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 3 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
target.Humanoid.Health = 0
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target then
attack(target)
break
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(destination.position - (Demon.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function Patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random (1, #waypoints)
walkTo(waypoints[randomNum])
end
while wait(0) do
Patrol()
end