Right now, I'm trying to write a piece of code that checks if an object (in this case, a trail) exists, and if it does, to destroy it. I've been trying to do that using an if/then statement, but it breaks after more than one use, or if the object in question never existed to begin with. Is there a way I can have my script check if an object exists without having it break down like that? Thanks in advance to anyone that responds.
✓ ANSWER ACCEPTED
Code element that checks whether an object exists?
2 answers
✓ Accepted answer
Example:
if game.Workspace:FindFirstChildWhichIsA("Trail", true) then -- Checks all descendants for a trail
local trail = game.Workspace:FindFirstChildWhichIsA("Trail", true)
trail:Destroy()
end
You can use the FindFirstChild function, in an if statement. You want to first find out if it exists using the functions, then you would destroy in the last part of the if statement. Something like this:
if workspace:FindFirstChild("FunnyPart") then -- checks if item exists
workspace:FindFirstChild("FunnyPart"):Destroy() -- destroys item
end
1 comment
0
why on earth do you use workspace when it's deprecated
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
FindFirstChild
as well as FindFirstChildOfClass("Trail")