C stack overflow?

Hello ,

So Im trying to create pathfinding NPC with @GrayzcaIe SimplePath module , but I encounter an error says C stack overflow… I tried to search in devforum but I can’t find any… It happen when I tried to stop and destroy the path when NPC died but I got error. Here is the script:

-- the function

local path

local function InitPath(root , goal)
	
	path = PathFinding.new(root)
	
	path:Run(goal)

	path.Visualize = true
	
	return path
	
end

-- when NPC died

DeadNew = child:WaitForChild("Humanoid" , 1).Died:Connect(function()

     path:Stop()
     path:Destroy()

end)

Any help is very appriciated :slight_smile: :slight_smile:

Instead of making this variable nil, you could just create a new Path object. Creating a new Path object every time you execute :Run() is an incorrect usage of the module. You instead create the Path object once and continue to execute :Run() after that.

Let me know how it goes.

1 Like

So I used this methods:

local function GetPath(root)
	
	local path = PathFinding.new(root)
	
	return path
	
end

local function InitPath(root , goal)
	
	local path = GetPath(root)
	
	path:Run(goal)
	
	path.Visualize  = true
	
	return path
	
end

RunServCon = Run.Heartbeat:Connect(function(dt)
				
				task.wait(dt)
				
				if not newchild:FindFirstChild("HumanoidRootPart") then
					
					repeat task.wait() until newchild:FindFirstChild("HumanoidRootPart")
					
				end

				if (char:WaitForChild("HumanoidRootPart" , 1).Position - newchild:WaitForChild("HumanoidRootPart" , 1).Position).Magnitude < 15 then
					
					InitPath(newchild , char:WaitForChild("HumanoidRootPart" , 1))
					
				else
					InitPath(newchild , mobspawn.Position)
				end
			end)

newchild:WaitForChild("Humanoid" , 1).Died:Connect(function()

       local path = InitPath(newchild , char:WaitForChild("HumanoidRootPart"))
				
 	   path:Stop()

end)

but I got this error attempt to index nil with "Velocity" and sometimes the script break after player died

Just to clarify, @GrayzcaIe meant to say that the Path object only needs to be defined once: when the script starts running. That however doesn’t account for the possibility that the NPC could spawn in a different location each time. If they spawn in the same spot between deaths and always heads to the same destination, only define it once.

1 Like

You mean something like this??

local function InitPath(root , goal)
	
	local path = PathFinding.new(root)
	
	path:Run(goal)

	path.Visualize  = true

	return path
	
end

and then define it like this

Run.Heartbeat:Connect(function(dt)

       InitPath(child , char:WaitForChild("HumanoidRootPart"))
      
end)

when the NPC died

local path = InitPath(child , char:WaitForChild("HumanoidRootPart"))
path:Destroy()

its very late rn in here …so i don’t know if my brain can function anymore

I meant only calling InitPath once the script begins running. Then you’d store the result in path.

It’s still early for me.

what do you mean

can you explain more??

this is how I run my function:

for i , newchild in pairs(Collection:GetTagged("Mob")) do
			
			RunServCon = Run.Heartbeat:Connect(function(dt)
				
				task.wait(dt)
				
				if not newchild:FindFirstChild("HumanoidRootPart") then
					
					repeat task.wait() until newchild:FindFirstChild("HumanoidRootPart")
					
				end

				if (char:WaitForChild("HumanoidRootPart" , 1).Position - newchild:WaitForChild("HumanoidRootPart" , 1).Position).Magnitude < 15 then
					
					InitPath(newchild , char:WaitForChild("HumanoidRootPart" , 1))
					
				else
					InitPath(newchild , mobspawn.Position)
				end
			end)

so I dont know what you mean by once the script begins running

I didn’t recognise you were using a third-party module (one with which the creator was willing to help you out with). The latest snippet of code you uploaded shows that you’re calling InitPath once every heartbeat. That’d probably take up a lot of memory first of all. In your :Died function, you defined a variable only accessible by that function then called :Stop immediately after it was created.

I would recommend redefining path as a global variable first of all. I think writing my own snippet would explain better, though I don’t know how that would work with your respawn dynamics.

local dest = Vector3.new() -- Don't judge me - I've never used the module before.
local char = script.Parent -- If your NPC's script is a child of the rig itself.
function InitPath(root , goal)
	local path = PathFinding.new(root)
	path:Run(goal)
	path.Visualize = true
	return path
end

local path
local hrp = char:WaitForChild("HumanoidRootPart", 1)
if hrp then
    path = InitPath(hrp, dest)
end
local humanoid = char:WaitForChild("Humanoid", 1)
if humanoid then -- Why have a timeout of one second if you fully expect the humanoid to exist?
     humanoid.Died:Connect(function()
          path:Stop()
          path:Destroy()
     end)
end

Screenshot (8)

so how do I make it global ??
use _G or something??

My mistake! I would add local to path in InitPath.

Its working well but the script broken when after player died

I got this error attempt to index nil with 'Velocity

at this line

 path = InitPath(hrp, dest)

I think I need to sleep … I’ll think about this tomorrow …btw thx for helping

1 Like

I’d hope @GrayzcaIe address proper case usage for when the Humanoid dies. I don’t believe this has been mentioned in the SimplePath post.