Protected call.
Lua has pcall, short for protected call. If you've used languages like C++, Python, ect you will know what try is. What pcall does is, receive a function as an argument, and call it in protected mode, so that runtime errors (errors that happen whilst your program is running) do not propagate and thus not terminate the program.
pcall's first return value is always a boolean. true is returned if the function was called successfully without any runtime errors. false is returned if there was an error.
- Everything else after the first return value is what the function returns. If
pcall returns false, the second return value is the error message.
- You can pass additional arguments and they will be the arguments to the function you have passed.
Example;
local function add(a, b)
return a + b;
end
local ok, result = pcall(add, 1, 2);
print(ok, result);
3 is the result of the add function and is returned by pcall.
Let's get an error.
local ok, result = pcall(add, 5, {});
print(ok, result);
The last value that was printed is the error message, and the first one is false because there was an error
You're better off waiting for Target2Name to exist via :WaitForChild though, but this answer is meant to answer this question of yours:
So, is there a way to make the error of Target2Name not being there yet not exist? i.e. catching it or something, I’ve come so far that I turned [...]