Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I format the message supplied from MessageOut into a proper table?

Asked by 8 years ago

I tried using the following pattern to get the script path, line number, and error:

local script,line,error = message:match("(.+):(%d+): (.+)")

And then to format it into a table I did the following:

local contents = {script = script, line = line, error = error}

However, I'm unsure of whether or not this method works properly, because it neither spit out an error nor worked correctly when trying to use the information. I added prints and the problem definitely revolves with this code. Can anyone please provide an alternative method?

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

After a few hours of testing, I came to the conclusion I was in fact incorrectly using an if statement that was unrelated to the code I thought was problematic. Here's is commented, well-explained code for anyone wondering:

game.LogService.MessageOut:connect(function(Message, MessageType) --\\Connect to the MessageOut event of LogService.

    if (MessageType == Enum.MessageType.MessageError) then --\\Make sure it's an error.

        local Script, Line, Error = Message:match("(.+):(%d+): (.+)") --\\Find the script hierarchy, line number, and the error. Equivalent to finding ANYTHING1:NUMBER: ANYTHING2 and assigning ANYTHING1 to the Script variable, assigning NUMBER to the Line variable, and assigning ANYTHING2 without the space before it to the Error variable. I suggest learning string patterns to understand how this works.

        local DotPosition = Script:find("%.") --\\Find the first . in the Script hierarchy.

        local Name = Script:sub((DotPosition + 1), #Script) --\\Get the name of the Script, by stripping the parent information. Note this only works for things that are a child of a child of DataModel (game). Doesn't work on things that are inside of something else. You can easily modify that however.

        --\\Add your code here :D

    end --\\End the if statement that makes sure it's an error

end) --\\End the event connection

If you need to put the values into a table, you can simply do this:

local Values = {Name = Name, Line = Line, Error = Error.} --\\Pack the values into a table.

If you want to put the values into the table directly, you can do this:

local Values = {Message:match("(.+):(%d+): (.+)")} --\\Directly pack the values into a table. More efficient!

However note method #2 won't strip the Parent information, so you can do that like so-

local Values = {Message:match("(.+):(%d+): (.+)")} --\\Directly pack the values into a table, like above. As I said, it's more efficient.

Values[1] = Values[1]:sub((Script:find("%.") + 1), #Values[1]) --\\I crammed it all into one line, so sorry for readability issues. What this does is strips the parent information (Note: still has the same problems as above. You can fix this easily on your own, though). If you need further explanation just drop a comment.
Ad

Answer this question