Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to find the current line of code you're on?

Basically I'm just wondering if it's possible for a script to find out what line of code it's on. For example, say you wanted to make a custom error handler, and have a function return (as a string value), what line of code the error was on. How would this be done? Is this even possible?

Here's what I made for my error handler so far, but it's kinda useless since it's just different ways to use the error function:

local function errorCheck(v,err)
	if not v then
		error("Error: "..tostring(err))
	end
	return true
end

print(errorCheck(nil,"the value is nil")) -- pretty much just the assert function

But instead of the script breaking, I'd want it to only return the line that the error is on, and that's it.

P.S:

LoadStringEnabled is disabled, and I don't want to use loadstring for this.

Simplified question:

How do I find the line of code a function call is on?

3 comments

0

I don't know any efficient methods, but you could redefine a variable at the beginning of every line equivalent to the line it's on, and print that on the error handler.

0

yeah but masta, that only fires if the code has already broken. i'm trying to find a way to get the current line of where ever i'm calling the function

3 answers

✓ Accepted answer

debug.traceback() is a function that provides the current call-stack (as a string).

Thus print( debug.traceback() ) will make something that looks fairly like an error message, where the first line is the current line, and the second is the caller, etc.

Here's a simple function which cleans up the trace. Warning: this will fail on many things, it makes a lot of assumptions.

function trace()
	local msg = debug.traceback()
	local t = {}
	for x in msg:gmatch("[^\n]+") do
		local source, line, message = x:match("^(.+):(%d+):%s+(.+)$")
		if message then
			table.insert(t, {source = source, line = line * 1, message = message})
		end
	end
    table.remove(t, 1) -- hide trace implementation
	return t
end

Keep in mind that scripts can have :s in their names, messages could have colons and newlines (maybe?), and that tail-recursive[^1] calls get weird output that this will ignore

[^1]: EDIT ROBLOX does not support tail calls, so this can be disregarded

If you pause the test in Studio, and you check the script, it should have a line in it, unless it is completely carried out, highlighted in yellow with a yellow pointer.

Hope this helped ;)

1 comment

0

I know how to debug a program e.e... My question is asking how to have a function return the line of code it's on.

I think this might be what you are looking for.


game:GetService("ScriptContext").Error:connect(function(message, trace, script) print(script:GetFullName().." errored!") print("Reason: "..message) print("Trace: "..trace) end


1 comment

0

This is for something that's already thrown an error. I'm trying to find the current line of code of a function call.

This discussion is preserved as it was. New answers, comments, and votes are closed.