Can I detect a script error?

Hello!

I was looking to try making a plugin for myself because I have no idea how they work, and thought this would be a good example.

local SES = game:GetService("ScriptEditorService")
local toolbar = plugin:CreateToolbar("Nerd Script Helper")
local NewButton = toolbar:CreateButton("🤓", "owo", "http://www.roblox.com/asset/?id=10218508810")
NewButton.Click:Connect(function()
	for i, v in ipairs(game:GetDescendants()) do
		if v:IsA("Script") then
			v:GetPropertyChangedSignal("Source"):Connect(function()
				print(v.Source)
			end)
		end
	end
end)

I was looking to make it detect a possible error, but I didn’t know how to do it.

How can I do it? Thanks!

2 Likes

Use pcall. It’s the only way to catch a script error in the same thread. If you don’t need that, use coroutine.

when you said “if v:IsA(“Script”) then”
You can put a else

I was looking to detect an error in the script that has been changed, where I put “print(v.Source)”

1 Like

You can try using loadstring or a module that emulates it to see if there’s any compilation errors. Try this:
StringLoader.rbxm (65.4 KB)

2 Likes

Wow that file is huge. Well atleast to me!

1 Like

Do you mean find an error in another script, before running the other script? Loadstring, without calling the returned function, is probably the only way.

2 Likes

It’s a programming language, it’s going to be massive.

1 Like

So wrapping loadstring in a pcall and then using the error argument to pcall would work?

1 Like

I mean the file like the one which was posted up there you know? the one i replied to.

1 Like

I believe so, yes. Definitely test it and see.
print(pcall(loadstring(“a”)))

2 Likes

And here’s the file size of a 450x450 PNG:

1 Like

Problem with loadstring is that it is disabled by default. I recommend using the module I sent you.

2 Likes

image
Did not work.

local SES = game:GetService("ScriptEditorService")
local toolbar = plugin:CreateToolbar("Nerd Script Helper")
local NewButton = toolbar:CreateButton("🤓", "owo", "http://www.roblox.com/asset/?id=10218508810")
NewButton.Click:Connect(function()
	for i, v in ipairs(game:GetDescendants()) do
		if v:IsA("Script") or v:IsA("LocalScript") then
			v:GetPropertyChangedSignal("Source"):Connect(function()
				local succ, err = pcall(function()
					loadstring(v.Source)
				end)
				if not succ then warn(err) end
			end)
		end
	end
end)
1 Like

How do I add this to the script?

You require it from the script, and use the variable from the module you just required as a function, specifying the string to load and the environment.

1 Like

When attempting to make it a local plugin I got an error.
image

I cannot read that that is very small even smaller then the script.

https://create.roblox.com/docs/reference/engine/classes/ScriptContext#Error

This service can detect script error

1 Like

You need to wait 1 second for the dependency to load.

1 Like