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)
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.
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 andalways heads to the same destination, only define it once.
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) -- [2024-07-31] Fixed spacing.
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