How would I extract a specific thing from a string?

Hello developers,

Basically what I am trying to do right now is make a custom log module. I have the basics such as console.log() and console.error(), but I added console.debug() to easily debug scripts, and this requires me to get the script line from debug.traceback(). How would I do this? The string that gets returned is like this: script_hierachy:script_line. Thanks if you can help.

2 Likes

Hmmm…This is JavaScript I guess. I ain’t so much used to JavaScript though but still I’ll try.

1 Like

Like you said, the string returned is script_hierarchy:script_line. You can use the string.split function to separate out the script line and script hierarchy appropriately, for example:

string.split(String, ":")

See string | Roblox Creator Documentation for more details.

2 Likes

This isn’t JS. This is a module that is meant to be like JS.

1 Like

Oh. So you may use string:split()

1 Like

This would work technically, though more info may be printed such as the function it was called on like so:

ServerScriptService.Script:2 function a

I’m guessing I could do this?

local splitTraceback = string.split(debug.trackback(), ":")[2]
local line = string.split(splitTraceback, " ")[1]

-- traceback:line info -> line  info -> line?
1 Like

Yep, using split separates everything in-between the character you’re looking for into a table. From what I believe you’re trying to do, you want to do

splitString[1]

which will return the second value in the table.

1 Like

Yeah, but I would suggest assigning a var before the splitTraceback and assign the returned value of the function to the var and split it.

2 Likes

I wouldn’t think I need to, as it doesn’t need to be a constant.

1 Like

There isn’t really any point to that, since debug.traceback() is only used once and is already easy to understand.

Oh, then it’s alright but sometimes it’s better to do it.

1 Like

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