generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: waitforchild()

Previous Thread :: Next Thread 
textmasterthe9th is not online. textmasterthe9th
Joined: 03 Oct 2008
Total Posts: 752
21 Jan 2013 02:30 PM
I was just wondering how would you use the waitforchild(), and what could it be used for?
Report Abuse
MainDirectory is not online. MainDirectory
Joined: 20 Dec 2012
Total Posts: 1834
21 Jan 2013 02:31 PM
Is wait for child a new method? Because this is the first I have heard of it.
Report Abuse
DrHaximus is not online. DrHaximus
Joined: 22 Nov 2011
Total Posts: 8410
21 Jan 2013 02:31 PM
It waits for a chi-

I don't know what's tripping you.
Report Abuse
textmasterthe9th is not online. textmasterthe9th
Joined: 03 Oct 2008
Total Posts: 752
21 Jan 2013 02:40 PM
Thankyou that was very helpful...
Report Abuse
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
21 Jan 2013 04:04 PM
@Main;

No, it's a function some people put in the beginning of the script. It would be used like this.

function waitForChild(parent,childName)
while not parent:findFirstChild(childName) do wait() end
return parent:findFirstChild(childName)
end

local baseplate = waitForChild(Workspace,'Baseplate')
Report Abuse
thecaptain97 is not online. thecaptain97
Joined: 17 Jun 2010
Total Posts: 4987
21 Jan 2013 04:05 PM
I just do this:

repeat wait() until workspace:FindFirstChild("BasePlate")



.-.
Report Abuse
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
21 Jan 2013 04:05 PM
It'd be better to put in a coroutine though I think.

function waitForChild(parent,childName)
coroutine.resume(coroutine.create(function()
while not parent:findFirstChild(childName) do wait() end
return parent:findFirstChild(childName)
end))
end
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
21 Jan 2013 04:06 PM
WaitForChild was said to be soon becoming a supported method, so it could be used like this:

script.Parent:WaitForChild("Hat")
Report Abuse
textmasterthe9th is not online. textmasterthe9th
Joined: 03 Oct 2008
Total Posts: 752
21 Jan 2013 04:52 PM
Thankyou :3
Report Abuse
eugenevorld is not online. eugenevorld
Joined: 15 Nov 2008
Total Posts: 310
21 Jan 2013 05:44 PM
As for what it could be used for, it can be used to halt the script until the required child is available eg.

WaitForChild("Player1",workspace)

Even though you can use the CharacterAdded event, this is still an alternative and can be used to wait for other instances that are required for your script to function as intended or else something like this...

local char = game.Workspace.Player1

just wouldn't work since Player1 does not exist but if you had used WaitForChild, it would pause the script until it is available.
Report Abuse
thecaptain97 is not online. thecaptain97
Joined: 17 Jun 2010
Total Posts: 4987
21 Jan 2013 06:07 PM
Or the old


repeat wait() until workspace:FindFirstChild("BOX")
Report Abuse
As8D is not online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
22 Jan 2013 06:59 AM
Now, what I use is both

WaitForChild(instance, name) and WaitForProperty(instance, name)

They're very useful, as some people above might have said, as they PAUSE the script/function until a certain child or property does exist/is set.

So, to sort something out: Using a coroutine would break the main reason for these functions, as they're, as the name tell, "waiting for a [child / property]".


Let's give the examples above a slight modification so we're sure you can see a difference:

--- Case 1, the source of a script, not using waitForChild ---

delay(5, function() local p = Instance.new("Part")
p.Name, p.Parent = "WeFindThisObject", workspace end)

part = workspace.WeFindThisObject

part.Anchored = true

------------------------------------------------------------------
----- Case 2, the source of a script, using waitForChild -----
function waitForChild(instance, name) if instance:FindFirstChild(name) then return instance:FindFirstChild(name) end
while true do instance.ChildAdded:wait() if instance:FindFirstChild(name) then return instance:FindFirstChild(name) end end end
function waitForProperty(instance, name) if instance[name] then return instance[name] end
while true do instance.Changed:wait() if instance[name] then return instance[name] end end end
-- Utility functions above, waitForChild & waitForProperty.

delay(5, function() local p = Instance.new("Part")
p.Name, p.Parent = "WeFindThisObject2", workspace end)

part = waitForChild(workspace, "WeFindThisObject2")

part.Anchored = true
------------------------------------------------------------------

The script that don't use waitForChild will likely give you this error (if you don't have a brick named the one in the example in workspace already):
scriptThing:7 - Anchored was not a valid member of nil. (or something like that)

While the function using waitForChild would wait 5 seconds, then anchor the brick in the example.


---> When can it become handy?
Well, if you operate with several scripts that need to get data from each other, need to remove the bodycolor when added to the character or something like that, then it's a huge adventage to already have the functions ready.

If you don't want to put them inside every script, you can use the global table _G to store waitForChild & waitForProperty, but beware, as the function can get a delay before indexed (You can then make a modified version of WaitForChild, like the WaitForProperty is, and use it to wait for a variable. Everything is possible!)



Sorry if this confused you,
- As
Report Abuse
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
22 Jan 2013 03:33 PM
WaitForProperty? Like this?

function WaitForProperty(inst, prop, value)
while inst[prop] ~= value do wait() end
end
Report Abuse
thecaptain97 is not online. thecaptain97
Joined: 17 Jun 2010
Total Posts: 4987
22 Jan 2013 04:05 PM
repeat wait() until game.Players.Bob.Character~=nil


I win
Report Abuse
IamAwesome777 is not online. IamAwesome777
Joined: 18 Jul 2011
Total Posts: 2640
22 Jan 2013 05:11 PM
repeat wait() until game.Players.Bob.Character

Shorter.

@epic: Don't use coroutines, because if you do it like this:

function WaitForChild(object, child)
repeat wait() until object:FindFirstChild(object)
end

You pause/yield the thread until the child is found. IF you use coroutines, the thread will keep running past the function, making the function useless.
Report Abuse
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
22 Jan 2013 05:53 PM
True, true.
Report Abuse
As8D is not online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
23 Jan 2013 09:29 AM
@1 below my previous comment;

No, not exactly like that, but it depend on what you want to use the function for. Remember, it's an user-made function, so you're free to change it ect, but we're talking about functions that have an always-useful purpose.


So: waitForProperty is much like waitForChild, but instead of using:
"instance:FindFirstChild(name)" then we're using "instance[name]" and instead of
"instance.ChildAdded:wait()", we're using "instance.Changed:wait()".

So, until the property isn't nil or false any longer, the script will wait.

This is how the "waitForProperty" function I use look like:

function waitForProperty(instance, name)
if instance[name] then return instance[name] end
while true do instance.Changed:wait()
if instance[name] then return instance[name] end
end end



Did you know, walking on a floating cube until your character has loaded is somewhat difficult. If your character loads slow, which is the problem.
- As
Report Abuse
epicfail22 is not online. epicfail22
Joined: 25 Sep 2009
Total Posts: 3739
23 Jan 2013 10:36 AM
I see.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image