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

How do I check if something exists while not getting an error when it doesn't?

Asked by
Songist 49
5 years ago

So right now, I have a part of a script that checks for a part, and if the part exists it makes a change to that part.

    if pl:FindFirstChild("RadioMesh") then
                    pl.RadioMesh.Transparency = 1
                end

It works fine. However, whenever I check the network, I get an error. It isn't a big deal, but it would be great if this didn't happen as it clutters the output and makes it harder to check for other errors.

So is there a way to go about checking for the existence of something without it outputting an error when it doesn't exist?

0
Network? and no, not without getting into pcalls and going overboard with custom meta userdatas, but there's no reason to do that when you have FindFirstChild() Azarth 3141 — 5y
0
azarth, what would cause this to error when you have findfirstchild? royaltoe 5144 — 5y
0
Sorry just meant the Network Simulator, mistyped it. Maybe people call it the output? Idk, I just get an error though since it looks for something that doesn't exist. If theres no easy fix thats fine, was just trying. Songist 49 — 5y
0
try adding : if pl:FindFirstChild("RadioMesh") ~= nil then ;;;;;;; elseif pl:FindFirstChild("RadioMesh") == nil then Clasterboy 72 — 5y
View all comments (2 more)
0
print ("Nil") Clasterboy 72 — 5y
0
I just realized there was a section with one of these that should have had findfirstancestor, since the its parent isn't always there, and changing that fixed it. Sorry I should have noticed that before! Songist 49 — 5y

1 answer

Log in to vote
0
Answered by
Songist 49
5 years ago

Well, instead of deleting it, it was suggested to answer my own question. Basically I wasn't careful when looking at the output and didnt realize the error was coming from a separate line where the thing I was checking for the existence of a child in also didn't alwasys exist, which was giving me the error.

So all I had to do was go back a level, so that I checked within the higher-up parent (sorry if theres a term for this) which always exists, and checked with findfirstancestor.

Ad

Answer this question