You can wrap any function in pcall()
, a Lua function that effectively "tries" to execute the code within it like Java and JavaScript's try
keyword. If you're not concerned about the error message (you probably aren't since you know what it will most likely be), you can just do this:
2 | local targets = { workspace.Part,workspace.OtherPart } |
You can also use if
statements to create a more conservative approach that does not require using the pcall
function:
1 | local targets = { workspace:FindFirstChild( 'Part' ),workspace:FindFirstChild( 'OtherPart' ) } |
3 | if targets [ i ] then targets [ i ] :Destroy() end |