Debug.traceback() won't work inside ScriptContext

Reproduction Steps
Run this:

local function c()
	print('c')
	error('MyError')
end

game:GetService("ScriptContext").Error:Connect(function(err)
	warn(err)
	print(debug.traceback())
end)

c()

Expected Behavior
debug.traceback() should show the entire traceback, like:

  Stack Begin
  Script 'Workspace.Script', Line 3 - function c
  Script 'Workspace.Script', Line 11
  Stack End

Actual Behavior
debug.traceback() is showing only it’s current line:

 Workspace.Script:8

Issue Area: Engine
Issue Type: Other
Impact: High
Frequency: Constantly

The debug.traceback function gets the stack trace of the running thread when provided with no thread, and the firing of a connection will create a new thread (or reuse the previous thread if no yields or errors occured). The result you’re getting from debug.traceback() makes sense when you consider how connections and debug.traceback work.

If you instead pass the thread to debug.traceback, you get the results you expected:

local function C()
	print"C"
	error"MyError"
end
local T = coroutine.running()
game:GetService"ScriptContext".Error:Connect(function(Err)
	warn(Err)
	print(debug.traceback(T))
end)
C()
--> ServerScriptService.Script:3: MyError
--> ServerScriptService.Script:3 function C
--> ServerScriptService.Script:10

For this case you can also use the second value provided to ScriptContext.Error, the traceback.

local function C()
	print"C"
	error"MyError"
end
game:GetService"ScriptContext".Error:Connect(function(Err,Trace)
	warn(Err)
	print(Trace)
end)
C()
--> ServerScriptService.Script:3: MyError
--> ServerScriptService.Script, line 3 - function C
--> ServerScriptService.Script, line 9
2 Likes

This topic was automatically closed after 7 days. New replies are no longer allowed.