Is there a function if it is wrong is the system?

Hello, for example a cube has the name “pepe” and then with a script it is changed to “pepe2”, and I have deleted the part.

script here:

game.Workspace.Camera.pepe:Destroy()
				
game.Workspace.Camera.pepe2:Destroy()

this in the same script, there is no way that if you make a mistake you go for the other ??

The Proper Method :angel::pray:

If you rely on a function that stops errors from breaking your code then you probably have an issue directly with how your code is laid out. I suggest you try to find a better way to do this than using an error-handling function as it does not seem appropriate for a case like this.

One way you can try fixing your script is to see if pepe exists, and if it does then destroy it.

local pepe = game.Workspace.Camera:FindFirstChild("pepe")

if (pepe) then
    pepe:Destroy()
end

--// then do the same thing for pepe2. However you might want to make a function for this to "reduce" the code

Alternatively if you do not need anything inside Camera then you can try clearing all of it’s children.

game.Workspace.Camera:ClearAllChildren()

The Error Handling Method :smiling_imp: :x:

Still don’t want to improve your code? Do this instead

There are 3 ways you can do this.

  • coroutine.wrap or coroutine.create
  • spawn
  • pcall

I suggest using pcall.

pcall creates a new “thread” for a function to run on. When the thread errors, the “main thread” that ran the pcall function will not.

Here is an example:

print"hello world"

local success, response = pcall(function()
    x = x + 1
    --// this will throw an error because we never defined `x`. 
    --// since `x` is not defined we will be trying to add `1` to a `nil` value. 
    --//that is why it errors
end)

print(success, response) --// see what happened in the pcall
print"ok done!"

This code will output:

hello world
false    4: attempt to perform arithmetic (add) on nil and number
ok done

Our code has ran successfully and no error that is wrapped in a pcall will stop and “thread” that started it.

Thanks so much!
Good luck!
Bye!