|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 21 Jan 2013 02:40 PM |
| Thankyou that was very helpful... |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 21 Jan 2013 04:05 PM |
I just do this:
repeat wait() until workspace:FindFirstChild("BasePlate")
.-. |
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| 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 |
|
|
| |
|
|
| 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 |
|
|
|
| 21 Jan 2013 06:07 PM |
Or the old
repeat wait() until workspace:FindFirstChild("BOX") |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 22 Jan 2013 03:33 PM |
WaitForProperty? Like this?
function WaitForProperty(inst, prop, value) while inst[prop] ~= value do wait() end end |
|
|
| Report Abuse |
|
|
|
| 22 Jan 2013 04:05 PM |
repeat wait() until game.Players.Bob.Character~=nil
I win |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
| |
|
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 |
|
|
| |
|