Hi! I’m currently trying to make this pathfinding npc not have a delay when “surrounding” the player.
Basically, the target (for testing purposes this target is the player at the moment) has a cylinder welded to it’s HumanoidRootPart and the npc keeps circling around random areas right outside of the cylinder.
Here’s the script, I need it to explain where I think the problem is located:
local Path
local PreviousWaypoint
local Part = workspace.Part -- The cylinder that is welded to the player
script.Parent.PrimaryPart:SetNetworkOwner(nil)
local function GetPath(destination)
Path = game:GetService("PathfindingService"):CreatePath({WaypointSpacing = math.huge, Costs = {TargetCylinder = math.huge}}) -- the cylinder has a PathfindingModifier labeled "TargetCylinder"
Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, destination)
end
while wait() do
local SetupWaypointCFrame = CFrame.new(Part.Position) * CFrame.Angles(0, math.random(0, 360), 0)
local WaypointCFrame = SetupWaypointCFrame + (SetupWaypointCFrame.LookVector * Vector3.new(Part.Size.Z - 5, 0, Part.Size.Z - 5))
GetPath(WaypointCFrame.Position)
for i, v in pairs(Path:GetWaypoints()) do
local GreatHeight = false
--[[if PreviousWaypoint ~= nil then --IRRELEVANT FOR MY PROBLEM
if PreviousWaypoint.Position.Y - v.Position.Y >= 20 then
GreatHeight = true
end
end]]--
PreviousWaypoint = v
if not GreatHeight then
script.Parent.Humanoid:MoveTo(v.Position)
script.Parent.Humanoid.MoveToFinished:Wait()
else
break
end
end
end
due to “script.Parent.Humanoid.MoveToFinished:Wait()”, the npc will always move to the next waypoint, no matter if it’s an outdated one. How can I break from this entire code and prevent it from waiting to complete the MoveTo when the cylinder/target moves?
I’ve been trying for quite a while and couldn’t find a solution, the best solution I thought of was to only run MoveToFinished:Wait() if the player’s position didn’t change, but that wasn’t very good at all.
Don’t worry, I found a bit of a solution to this but it still has some problems. This post helped me a lot.
Here’s what I’ve done:
local Path
local PreviousWaypoint
local Part = workspace.Part
local Moving = false
script.Parent.PrimaryPart:SetNetworkOwner(nil)
local function GetPath(destination)
Path = game:GetService("PathfindingService"):CreatePath({WaypointSpacing = math.huge, Costs = {TargetCylinder = math.huge}})
Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, destination)
end
while wait() do
local TargetCFrame = workspace.Part.CFrame
local SetupWaypointCFrame = CFrame.new(Part.Position) * CFrame.Angles(0, math.random(0, 360), 0)
local WaypointCFrame = SetupWaypointCFrame + (SetupWaypointCFrame.LookVector * Vector3.new(Part.Size.Z - 5, 0, Part.Size.Z - 5))
GetPath(WaypointCFrame.Position)
for i, v in pairs(Path:GetWaypoints()) do
--[[local GreatHeight = false --IRRELEVANT FOR MY PROBLEM
if PreviousWaypoint ~= nil then
if PreviousWaypoint.Position.Y - v.Position.Y >= 20 then
GreatHeight = true
end
end
PreviousWaypoint = v]]--
if not GreatHeight then
script.Parent.Humanoid:MoveTo(v.Position)
Moving = true
repeat wait()
script.Parent.MovingEvent.Event:Connect(function(bool)
Moving = bool
end)
until TargetCFrame ~= workspace.Part.CFrame or Moving == false
else
break
end
end
end
(It’s very undecisive when it’s not around the cylinder)
Good part is that I think I have a solution for that, which is making the ai follow the player itself instead of waypoints if there’s a certain distance between player and NPC and line of sight is established, bad part is that I’m pretty sure that’ll take some complex coding (or I could be wrong, we’ll see).
Your WaypointSpacing is set to math.huge, which creates only 2 waypoints; start and end. This makes pathfinding service useless, since you can achieve the same thing by only :MoveTo(WaypointCFrame.Position). Are you sure this is intentional? If so, just do :MoveTo in a loop to the destination.
Just noticed and changed that some seconds ago lol. The only reason I did that is because I wasn’t really sure of what to set so I just changed it back to default. That and making sure the first waypoint never plays fixed the undecisive NPC problem, thank you!