Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you detect an instance in a part?

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

✓ Accepted answer

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!

1 comment

0

That's what I ended up using. I used WaitForChild to remove the instance.

If part:FindFirstChild("NameOfInstance") then -- This will search through the part for an instance.

2 comments

0

Lol way too simple.

0

It's what he asked for...

This discussion is preserved as it was. New answers, comments, and votes are closed.