How to catch all errors

it is possible to catch all errors without stopping a script?
if you are curious, im using a single script architecture (1 script for server and 1 localscript for client)
is it possible to catch errors so i can display them in a separate window (basically a gui) so ppl can report these problems with ease

2 Likes

pcall allows you to do that

local succ, err = pcall(function()
	--// your script and stuff
end)

print(err or "Script ran successfully")

if the code inside the pcall will error it will print it and rest of the script will continue running

what if ill use pcall around a loop? or event? will the loop break after catching an error?

A lot can definitely be done with LogService | Documentation - Roblox Creator Hub to display output.

However, there’s no efficient and practical way to have a script that just catches all errors. pcall()s are very nice, but they shouldn’t be used excessively. They are designed for anything that is prone to errors (especially web requests) and out of developer’s control, rather than preventing something that a developer can handle. They are also a bit more expensive than a normal function call.

2 Likes

Nope. Any error within a pcall will NOT yield the script outside of the pcall.

while true do
    local succ, err = pcall(function()
        error()
    end)
    task.wait(1)
end

Loop will continue running

what if ill do

local succ, err = pcall(function()
  while true do
      task.wait(1)
  end
end)

If you have it like

local succ, err = pcall(function()
  while true do
      error()
      task.wait(1)
  end
end)

then the loop will end when it reaches the error

how exactly log service works? is it like output in studio?

1 Like

Relatively similar, it can help you list the output. There are also some examples in the docs.

Edit. Perhaps you can also use string.match() and string patterns to find key words like error and extract the stack trace.

1 Like

does it work on server side? cuz documentation says about client
Edit. It works indeed, thanks for pointing me out

2 Likes

Yes, I don’t know why the documentation phrased it like that, the actual situation is that each machine has its own output. LogService.MessageOut on server will only be seen by the server, and client output will only be visible locally.

No problem! If I can help any further, let me know. My DMs are open too. :slight_smile:

1 Like

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