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

How do you detect an instance in a part?

Asked by
Spooce 78
9 years ago

I'll be using and if then and I want to see if an instance is in a part and if it is, I want it to be replaced with a new instance. Help?

2 answers

Log in to vote
1
Answered by 9 years ago

As RedCombee has answered, why not use the FindFirstChild Method? If the FindFirstChild method finds the Child, then it'll return true, elseif could not find the Child, then it'll return nil, here's an example of what I mean;

local child = game.Workspace:FindFirstChild("Child") --Here's 'FindFirstChild'; this will check to see if 'Child' is existant in the Workspace

if child then --If 'child' is existant
print("Child found!") --It'll print this in the Output
else --Elseif 'child' is not existant then
print("Child not found!") --It'll print this in the Output
end

Or, you could do FindFirstChild("Child",true), that will check the game, even if 'Child' is in a 'Model' type instance, it'll still beable to tell if existant or not, here's an example of what I mean;

Game
    Workspace
        Model
            Child

Now, let's use the FindFirstChild("Child",true) method;

local child = game.Workspace:FindFirstChild("Child",true)

if child then --If 'child' is existant
print("Child found!") --It'll print this in the Output
else --Elseif 'child' is not existant then
print("Child not found!") --It'll print this in the Output
end

And, as you'll see, even though it's not exactly in the 'Workspace', it'll still beable to find it when you add ,true in with the FindFirstChild method. Hope this helped!

0
That's what I ended up using. I used WaitForChild to remove the instance. Spooce 78 — 9y
Ad
Log in to vote
0
Answered by
RedCombee 585 Moderation Voter
9 years ago
If part:FindFirstChild("NameOfInstance") then -- This will search through the part for an instance.
0
Lol way too simple. bobafett3544 198 — 9y
0
It's what he asked for... RedCombee 585 — 9y

Answer this question