Stack overflow on weird places in my script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a pathfinding bank with spawning/despawning civils on the streets.

  2. What is the issue? Include screenshots / videos if possible!
    After some time, the output slaps me with:

Workspace.NPCs.Civils.Civilian.Pathfind_Civilian:61: stack overflow

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

No, I didn’t. I don’t think I would find anything there though.

So it errors at a pathfinding script in ServerScriptService, which looks like this:

I put the entire script in here

It errors on line 61, which is an if statement

local dummy = script.Parent
dummy.PrimaryPart:SetNetworkOwner(nil)
local PathFinding = game:GetService("PathfindingService")

local lastWaypoint = nil

local onBreak = false


local agentParams = {
	["AgentHeight"] = 5,
	["AgentRadius"] = 5,
	["AgentCanJump"] = false
}

local function createPath(destination : BasePart)
	local path = PathFinding:CreatePath(agentParams)
	
	path:ComputeAsync(dummy.HumanoidRootPart.Position, destination.Position)
	
	return path
end

local function moveTo(target : BasePart)
	local path = createPath(target)
	
	for i, obj in pairs(workspace:GetDescendants()) do
		if obj:IsA("Seat") then
		
			if obj.Occupant == dummy.Humanoid then
				obj:Sit(nil)
				task.wait(0.1)
				dummy.PrimaryPart.CFrame = obj.Waypoint.Value.CFrame + Vector3.new(0, 2, 0)
			end
		end
	end
	
	for i, waypoint in pairs(path:GetWaypoints()) do
		dummy.Humanoid:MoveTo(waypoint.Position)
		dummy.Humanoid.MoveToFinished:Wait()
	end
	
	if target.Name == "SeatWaypoint" then
		task.wait(0.1)
		target.Seat.Value:Sit(dummy.Humanoid)
	end
	
	game:GetService("TweenService"):Create(dummy.HumanoidRootPart, TweenInfo.new(0.2), 
	{CFrame = target.CFrame + Vector3.new(0, 2, 0)}):Play()
	
	if target.Name == "DespawnWaypoint" then
		target:SetAttribute("Occupied", false)
		game.ReplicatedStorage.DespawnTouched:Fire(dummy)
	end
end

local function randomize(location : Folder)
	
	local waypoints
	
	if #location:GetChildren() ~= 0 then -- the line the output tells me
		waypoints = location:GetChildren()
	else
		warn("Location does not have any waypoints")
	end

	local goal = waypoints[math.random(1, #waypoints)]
	
	if goal:GetAttribute("Occupied") == false and goal ~= lastWaypoint then
		
		for i, point in pairs(waypoints) do
			point:SetAttribute("Occupied", false)
		end
		
		goal:SetAttribute("Occupied", true)
		
		return goal
	else
		return randomize(workspace.Waypoints_Civil.CivilianWaypoints)
	end
end

local goal = randomize(workspace.Waypoints_Civil.CivilianWaypoints)

moveTo(goal)

lastWaypoint = goal

task.wait(5)



while wait() do
	task.wait(math.random(3, 5))
	
	local goal = randomize(workspace.Waypoints_Civil.CivilianWaypoints)
	
	moveTo(goal)
end

This code shows the newer script, because it overflowed on a GetChildren() function before. I absolutely have no idea how could this happen. Thanks in advance!

This function is recursive and can cause stack overflow from inability to fulfill the if-statement.

1 Like

I don’t exactly understand. If you’re talking about the returns in the randomize function, then they were set like this so the dummy wouldn’t choose the previous waypoint. I’m not sure how to fix that.

Maybe I should try putting wait() here and there? I’ll check.

Um… I don’t know how to say this…

I kinda have a different problem now.

Hooray

Now it doesn’t put anything I can fix, and my output is like this:

Now it’s even harder to fix

Bruh

Okay, I fixed this one too.

Turns out I was destroying the civilian too quickly when despawning, and it interrupted a CFrame line.

All good now!

Thank you @anon81993163 for helping!

1 Like

Your problem is in this line:
if #location:GetChildren() ~= 0 then – the line the output tells me

This will always be true because #location:GetChildren() is always 0. It is the same as doing this:
local children = location:GetChildren()
local childCount = #children
if childCount ~= 0 then – the line the output tells me

You need to use this instead:
if #location:GetChildren() > 0 then – the line the output tells me

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