SimplePath module: Trouble with cancelling a path

Here’s a link to the pathfinding module I’m using.

Whenever Lighting.ClockTime changes, I go through all NPCs and call a module NPC_Pathfinding

			for _, npc in pairs(CollectionService:GetTagged(activeZone.Name.."_1")) do
				if targetDestination then
					npc:SetAttribute("target", targetDestination)
					local path = NPC_Pathfinding.new(npc, targetDestination)
					path:walk()
				end
			end

Finally, in this module, I call the SimplePath module.

function path:walk()
	self.Dummy.AttributeChanged:Connect(function(attributeName)
		self.Path:Destroy()
	end)

	self.Path:Run(self.Goal)
end

As you may notice, whenever attributes assigned to the npc (self.Dummy) changes, the SimplePath Path is destroyed, so that the NPC will go to the new target instead.

Now everything appears to work fine in-game, but I keep having these errors thrown at me and it’s really pissing me off having to see this in my output constantly. Does anyone have a clue why this keeps happening?
image

This is where the first error comes from
image

And the second error is from the SimplePath module (line 286)

Bump

1 Like

Why are you trying to destroy the path? SimplePath returns a table that does not have a destroy function, but if it did, then you would need to create a new path in order to call the :Run function, because generally, :Destroy means to get rid of something completely. How can you use something, if it has been gotten rid of?

EDIT: The objects returned by SimplePath do have a Destroy function, my mistake!

Also, :Run is designed to be “spammable”, and there is actually an example on the official documentation which is just a while loop with :Run

Method 2: Using Loops

In the following tutorial, you will learn how to use SimplePath using loops instead of events.

Using SimplePath in a loop is way simpler than using events. You only need 3 lines of code:

while true do
    Path:Run(Goal)
end

Anyway, the fix for this is to remove the :Destroy line:

self.Path:Destroy()

You could go one step further, however. The fix could lead to a build-up of SimplePaths that accumulate over time because you could be constantly telling the NPCs to go somewhere, so, you could:

not do this. You can instead have NPCs create a path once, and then use BindableEvents to tell them to update their pathing (although, SimplePath is meant for constant path re-calculation, so you could just have a constant while loop instead so that BindableEvents are not needed), for example:

for _, npc in pairs(CollectionService:GetTagged(activeZone.Name.."_1")) do
	if targetDestination then
		npc:SetAttribute("target", targetDestination)
		npc.UpdatePathing:Fire()
	end
end
-- npc code, you could also have an "npc manager"
-- that keeps a list of all the npcs and all the SimplePaths.
-- that would be better if you have lots of npcs (like, more than 50 IMO),
-- because having too many scripts can cause lag or slow them down.

npc.UpdatePathing.Event:Connect(function()
	path:Run(npc:GetAttribute("target"))
end
2 Likes

Thanks for the reply (it seems like I’m getting less and less the more I progress these days), but:

is untrue, refer to the API

But your suggested idea in the latter half of the post was indeed useful, although I didn’t use any of your specific suggestions, it still led me to an answer, thank you.

And for anyone who’s wondering, I created a table that stores the NPC_Pathfinding (OOP) class for every NPC in the game. Then whenever the in-game time changes, I check if the current time matches a new target (i.e, at night let the npc go to bed) then call the walk method.
Here’s some pseudo-code to get a general sense of what I did:

NPCPaths = {}

function playerjoins()
   for _, npc in ListOfNPCs do
       NPCPaths[npc.Name] = NPC_Pathfinding.new(npc) 
   end
end)

function LightingChanged()
    for _, npc in ListsOfNPCs do
        -- check if we have a new target
        -- if we do, set target to new target
        -- then call walk method
        NPCPaths[npc.Name]:walk(target)
    end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.