I want to create a script that detects errors and shows sources of said errors so that said script can automatically remove it/disable it/do anything to it. So far I’ve been using scriptcontext’s error function, although it only outputs a string that I am unable to convert into a path to the source. Do you know any alternatives, any solutions to this problem?
well i dont really know but if you could find a way to place it all in a pcall then you could get the errors, though i dont know how well this would work
Well I have no clue on how to use pcalls to find the sources of errors and then get a ready path to use for modification of said errored script.
Pcalls - When and how to use them
How does pcall, xpcall, and ypcall actually work? [closed] - Scripting Helpers
try reading through these but basically pcalls are used for finding errors in a cetain function, most commonly datastoreservice as it can fail quite a bit
for example
local success, err = pcall(function()
workspace.Brick.Color = BrickColor.new("Really red")
end)
if not success then
warn("There was an error //"..err)
end
-- this would return an error as brickcolor is not a color3 value
local success, err = pcall(function()
workspace.Brick.Color = BrickColor.new("Really red").Color
end)
if not success then
warn("There was an error //"..err)
end
-- this one wont return an error as .Color for brick color will return the color3 value
if you wanted info about the datastore you could read through
Data Stores | Documentation - Roblox Creator Hub
though about the find sources thing you would need to make the error finder as a plugin,
then you would just copy the source of a script and in a seperate script place the source in a pcall. i dont know much about plugins but that would be how you would do it
oh dude if a computer could solve this we would be out of work! look into the halting problem
Have you tried LogService.MessageOut?
A example of this is
game:GetService("LogService").MessageOut:Connect(function(Message, Type)
if Type == Enum.MessageType.MessageError then
-- Is a error, you can send the error to a server
end
end)
You missed the point entirely lol. I don’t want a script that fixes the errors in other scripts by a miracle. I just want a script that traces the error to where it came from with the ability to make a path to it, like: “workspace.Model.Script”.
It does not meet my requirements. It can’t be used to trace the error back to where it came from in a way that creates a path to the script.
No. I do not need to make it into a plugin. I have no clue where you got that idea from. From what I read, pcall again does not meet the expectations, as I am unable to code it to find an errored script and trace it making a usable path. I don’t know if it’s because I don’t know how to do it, or because pcall just can’t do it.
It can, It traces any type of Messages that are printed to the output, you can either get every single message printed or get a specific one, I recommend to check this out. MessageType | Documentation - Roblox Creator Hub
From my understanding of it, you are wrong. It does not trace the source of the error script. It only prints the message type, such as if it’s an error. It can also print a reason for why it errored, but it will never give you the path to the source.
Incorrect, When your script errors it prints the line it errored on meaning you could use it to Log that message which will show you the line the script errored on.
" Head is not a valid member of Model “Workspace.Enemy” "
That’s all I’m going to cite to prove you wrong. This error message only says that head is not a valid member of a model with the following path. See the problem? It cites the path (even though it’s at best a string, not a path) to the wrong thing. It doesn’t make a path to the script that errored. It instead made a “path” to what the errored script was looking for and didn’t find anything in. Seriously, stop giving bad advice when someone tells you that it’s not it, logservice doesn’t work in this case.
PS. Don’t tell me how to fix that particular error, because it was an example error just to prove that your “solution” is not in fact a proper solution.
You dont have to use Enum.MessageType.MessageError… there is different types of MessageType, I believe the one youre Looking for is MessageInfo not sure I forgot but you would need to test all of them.
I have already told you. This is not the solution. It doesn’t bring a path to the errored script. It only describes what kind of message it is. You even linked it yourself, haven’t you read it?
well log service does actually have a way to get the path, whether or not I remember how is something else but there should be a way
Well, then I have no clue what that way can be. If you are right, then we still don’t know how to do it, we just know it’s possible.
As @56ytr56 said it is possible to get the actual script from the error source string. I’ve quickly written together this script with details in it for how it works. Do note that it might not be 100% reliable but when I tested it in studio it seemed to work fine.
-- Get the log service:
local logService = game:GetService("LogService")
local function GetScriptFromPath(path)
local current = game
-- Loop through the splits of the string (Say it is ServerScriptService.Example then path:split(".") returns {"ServerScriptService", "Example"}:
for _, name in ipairs(path:split(".")) do
-- Get the instance inside the current to allow for deep searching:
current = current:FindFirstChild(name)
-- If it cannot find the script then stop looking and show a warning in output:
if not current then
warn("Script could not be found")
return nil
end
end
-- Return the script found so it can be used:
return current
end
local function GetPathFromString(source)
-- Split the source by : to get the path
source = source:split(":")
-- Path should always be the first index in the table returned by the split
local path = source[1]
-- Return the path or an empty string if no path is found to avoid issues:
return path or ""
end
local function ScanMessage(message, messageType)
-- Confirm it is an error:
if messageType ~= Enum.MessageType.MessageError then return false end
-- Get the relevant path:
local path = GetPathFromString(message)
-- Get the script from the path:
local scriptContainer = GetScriptFromPath(path)
if scriptContainer then
-- Disable the script if it exists:
scriptContainer.Disabled = true
end
end
-- Listen for when a message is added and scan it:
logService.MessageOut:Connect(ScanMessage)
-- Get the already existing messages:
for _, messageData in ipairs(logService:GetLogHistory()) do
-- Scan the existing messages:
ScanMessage(messageData.message, messageData.messageType)
end