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

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

Asked by
Scaii_0 145
6 years ago

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.

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. User#5423 17 — 6y
0
so something like: playergui:FindFisrtChild(Gui) if Gui = nil then???? Scaii_0 145 — 6y
0
if playergui:FindFisrtChild(gui name here) then -- found else -- not found end User#5423 17 — 6y
0
only nil and false are considered false in Lua everything else is considered true. User#5423 17 — 6y

1 answer

Log in to vote
0
Answered by
Aimarekin 345 Moderation Voter
6 years ago

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

Ad

Answer this question