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

How would you continue a script if a child is NOT found?

What I am trying to achieve here is basically:

When the player touches this part (working) I need to check if a Gui exists in the player (trying to achieve)
And if it DOESN'T, then continue with the script (also trying to achieve) If it DOES, then finish (also trying to achieve)

Right now I have a duplicated GUI where one is confused what to do.

The game also has Filtering Enabled.

4 comments

0

FindFirstChild will return the child with the given name or nil so you can use an if statement to run code which it no dependent upon this child.

0

so something like: playergui:FindFisrtChild(Gui) if Gui = nil then????

0

if playergui:FindFisrtChild(gui name here) then -- found else -- not found end

0

only nil and false are considered false in Lua everything else is considered true.

1 answer

✓ Accepted answer

FindFirstChild finds an object that has the the parent inidcated with the name you indicate.

For example...

game.Workspace:FindFirstChild("PartName") -- game.Workspace is the parent where we are searching and PartName is the name of the part.

If the script doesnt find anything with that name, it will return nil(Which in Lua means nothing).

game.Workspace:FindFirstChild("AnameThatDoesntExist") -- This will return nil

So, we could do...

if game.PlaceWhereYouWantToSearch:FindFirstChild("PartName") == nil then
-- Script to do when there isn't that object
end

If you want to check if a ScreenGui is there, insert a localscript inisde StarterGui:

if script.Parent:FindFirstChild("GuiName") == nil then
-- Script to do when there isn't that ScreenGui
end

Hope it was helpfull! If you have any doubts, tell me. Wiki article: http://wiki.roblox.com/index.php?title=API:Class/Instance/FindFirstChild

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