How to get the current script line number

For debugging purposes, I’d like to know which line a given statement in my script is on.
For example, if the statement below is on line 123, it should print 123:

-- the line below is 123, so it should print "123"
print('The current line of this print is', *CurrentLine*) 

I know I have debug.traceback but it will print the whole traceback, not just the current line.
Is it possible?

2 Likes

You cant. Roblox doesn’t provide the debugging function required from debug to get the current line of a statement

You can do it with debug.info, see focasds post below, I still have a suspicion it wont work under O2 mode though

1 Like

I believe debug.info would be helpful here! :smiley:

The following code gets the line number and prints it out:

print(debug.info(1, 'l'))

Image Sample:
image

4 Likes

You can just use a trick like this:

local line = debug.traceback()
print(string.match(line, "%w:(%d+)"))

As the debug.traceback function would have this pattern before the line number.

1 Like

Can you put --!optimize 2 at the top, I’m curious if this still works when LINEINFO is disregarded by the compiler

Like that? It printed 16 still.

1 Like

Interesting yeah (it’d be complaining if optimize wasn’t a valid directive)

I was mistaking debug level for op levels anyway haha.

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