Raycasting "stack overflow"

I have ~10 balloons that float around a room and bounce off the walls. Every few minutes or so, Studio crashes for about 20-30 secs and causes this error repeatedly on some balloons, not all. Sometimes it’s about ~3 different balloons but keeps spamming the error on one specific balloon many times if that makes sense.


Script:

function Shoot()
	local maxDistance = 999999999999999
	local curDistance = 0
	local stepDistance = .02
	local stepWait = 0
	local currentPos = script.Parent.Position
	local currentNormal = script.Parent.CFrame.LookVector

	local function Step(overrideDistance)
		local params = RaycastParams.new() --Line 14
		local direction = currentNormal * (overrideDistance or stepDistance)
		params.FilterType = Enum.RaycastFilterType.Exclude
		params.FilterDescendantsInstances = {script.Parent}
		local result = workspace:Raycast(currentPos, direction)
		local pos
		local currentPos2 = Vector3.new(currentPos.X, 9.875, currentPos.Z)

		if result then
			pos = result.Position
		else
			pos = currentPos2 + direction
		end

		script.Parent.CFrame = CFrame.new(currentPos2:Lerp(pos, 0), pos)

		local oldPos = currentPos
		currentPos = pos

		if result then
			-- r = d - 2(d DOT n)n
			local norm = result.Normal
			local reflect = (currentNormal - (2 * currentNormal:Dot(norm) * norm))
			currentNormal = reflect
			Step(stepDistance - (pos - oldPos).Magnitude) --Line 42
			
			local initialY = 9.875
			script.Parent.Position = Vector3.new(script.Parent.Position.x, initialY, script.Parent.Position.z)

			return
		end

		curDistance = (curDistance + (pos - oldPos).Magnitude)

		-- Recurse if max distance not reached:
		if curDistance < maxDistance then
			task.wait(stepWait)
			Step() --Line 62
		end
	end

	Step()

	warn("Balloon stop moving")
end

local randomy = math.random(0,360)
script.Parent.Orientation = Vector3.new(0,randomy,0)

Shoot() --firee!!!!!!

Unsure of why it happens and can’t really find anything online. Help please n thanks.

1 Like

I believe raycasts can only go 15,000 studs in a single direction. You set the max distance to something well above that, so perhaps the recursion is causing a stack explosion when you do not hit any objects since the condition curDistance >= maxDistance will not occur.

Stack Overflow happens when you call repeatedly something too fast.

This is why.
Set it to something lower like 1/20, or a changing number in the ranges of 1/60 to 1/10 that changes depending on how many balloons exist.

Although personally, I’d say runService.Heartbeat(deltaTime) would be better to use for this, as if stepping lags, you can compensate for it by multiplying values by deltaTime.

1 Like

That would only delay the stack overflow – it doesn’t matter how “fast” you call it – it occurs when functions recursively call each other, adding too many items to the stack and hitting the limit of 20,000 Lua calls.

@davidsusbus Redesign your system to bind or connect to a frame event – Heartbeat/PostSimulation (the latter is the newer equivalent) – like @Chark_Proto said.

2 Likes

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