Quick question! Right now I'm using an if statement:
if game.Workspace.part ~= nil then -- Do something end
If the part doesn't exist, then I'm getting an error because it is looking for the part to check if it is nil. It works in the sense that the code inside won't run unless the part does exist, but I'd rather have fully functional and optimized code.
So that leads me to my question- what should I be using instead?
do this if you want a child with a specific name:
if workspace:FindFirstChild("Part") then -- use workspace, game.Workspace is deprecated end
if you want to find a child that is a part then do this
if workspace:FindFirstChildOfClass("Part") then end
if you want all the children that are for example a tool or a part then do this:
for i,v in pairs(workspace:GetChildren()) do if v:IsA("Part") then -- use v instead of part now end end
You could:
if workspace:FindFirstChild('part') then print('part exists') end -- Or if not workspace:FindFirstChild('Part') then print("part doesn't exist :c") end
Hopefully, this helped, best of luck developer!
Edit: Fixed the string