I need some help
(This might be kind of a long post so keep that in mind)
For 4 straight days I’ve been working on a Pathfinding NPC System, I’ve made improvements, but I couldn’t even get the main pathfinding system to work properly.
I don’t want to use pathfinding resource modules, because they aren’t as customizable as If I made my own. (I did try SimplePath)
(I’ve tried countless things, gotten help from DevForum posts, and youtube tutorials, but every single thing I tried had a flaw in it)
This is the main pathfinding script
function data:FollowPath(target:BasePart)
task.spawn(function()
Following = true
-- Constant following
while true do
local initPos = target.Position
print("got position!")
-- If not closed then
if Following == true then
-- Compute the path
data.Path:ComputeAsync(character.HumanoidRootPart.Position, target.Position)
-- If the path was created successfuly
if data.Path.Status == Enum.PathStatus.Success then
local wps = data.Path:GetWaypoints()
-- Incase the path was blocked
data.Events.DefaultEvents.BlockedPath = data.Path.Blocked:Connect(function(blockedWPindex)
if blockedWPindex >= NextWaypoint then
data.Events.ManualEvents:End()
data:FollowPath(target)
end
end)
-- Visualize option
if data.Settings.Visualize == true then
task.spawn(function()
if CurrentVisualizedWaypoints ~= nil then
EF:ClearWaypoints(CurrentVisualizedWaypoints)
end
for i, waypoint in ipairs(wps) do
local part = Instance.new("Part")
part.Shape = Enum.PartType.Ball
part.Color = Color3.new(0.682353, 1, 0.760784)
-- Different actions
if waypoint.Action == Enum.PathWaypointAction.Jump then
part.Color = Color3.new(0.219608, 0.662745, 1)
elseif waypoint.Action == Enum.PathWaypointAction.Custom then
part.Color = Color3.new(1, 0.419608, 0.827451)
end
part.Material = Enum.Material.Neon
part.CFrame = CFrame.new(waypoint.Position)
part.Parent = workspace.Cache
part.Name = i
part.Anchored = true
part.Size = Vector3.new(0.42,0.42,0.42)
part.CanCollide = false
table.insert(CurrentVisualizedWaypoints, part)
task.delay(4, function()
if part then
part:Destroy()
end
end)
end
end)
end
-- // Move Behaviour
for i, waypoint in pairs(wps) do
-- If need to jump
if waypoint.Action == Enum.PathWaypointAction.Jump then
character.Humanoid.Jump = true
end
character.Humanoid:MoveTo(waypoint.Position)
character.Humanoid.MoveToFinished:Wait()
end
else
task.wait()
end
else
break
end
end
end)
end
(It’s inside of a module)
(I’ve tried way too many things to list them all)
This current code of course has a flaw, the path updates too slowly, making the NPC easy to avoid
(I’ve seen people make the pathfinding NPC stop using pathfinding and just move to the target once it’s close, but I’d rather not do it that way.)