Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 18 Jul 2012 10:50 PM |
I have been advised to put a wait(1) before LocalScripts so everything properly initializes. I know there is some truth to this because I have seen errors where script.Parent errored or script.Parent.Object errored even though there was an Instance named that Parented to script.Parent at the time.
Can someone explain the reasoning behind the initialization errors, and what I can do to prevent them? Will game ever be nil? Will game.Workspace or Workspace ever be nil? Is it fine to do game.Lighting.BlaBla but not script.Parent.BlaBla? Help! |
|
|
| Report Abuse |
|
|
| |
|
|
| 18 Jul 2012 11:08 PM |
| I personally have never worked with ROBLOX's in-game API before, but I do think a bug reporting system would be helpful in situations like this. |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 19 Jul 2012 12:39 AM |
"what I can do to prevent them?"
"Putting a wait(1) in LocalScripts" |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 19 Jul 2012 12:45 AM |
game, Workspace, script, etc.. will always exist for sure.
What won't is things that have to be loaded from the server. They could take any amount of time to load, even a few minutes! (Although that's highly unlikely, and the server would probably kick you if your connection was running that slowly).
wait(1) isn't enough. You need to actually wait for the objects that you need to arrive. The only objects that you can trust to be there for sure right when a localscript starts are the direct ancestors of a script (.Parent, .Parent.Parent, etc). All other things should be explicitly waited for if you never want your code to fail.
Reasonably you don't have to wait for long-running game objects, such as the terrain and other static parts, as they'll have arrived long before any scripts of yours got a chance to run.
What you _do_ have to wait for is two main cases:
-If you have a localscript in a tool, when a player first spawns, the script may arrive before it's children and/or siblings do. For instance, if you have some configuration and state values in the tool, such as Ammo and Range, you must wait for them, as the script may start running before they arrive on the client.
-If you have some other localscript that you clone into a player or elsewhere to do local effects like shaking a camera, and that localscript has children. As in the other example, the localscript's children may not have arrived yet when the script starts running.
How do you fix it? It's not that hard, just wait for the objects to exist. For example:
while not script:FindFirstChild('Config') do script.ChildAdded:wait() end |
|
|
| Report Abuse |
|
|
|
| 19 Jul 2012 02:55 AM |
As stravant said, you could make a WaitFor() function to wait for the necessary items before the script starts runnig.
☜▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬☜☆☞▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬☞ - Candymaniac, a highly reactive substance. |
|
|
| Report Abuse |
|
|
SN0X
|
  |
| Joined: 24 Oct 2011 |
| Total Posts: 7277 |
|
|
| 19 Jul 2012 03:22 AM |
function WaitForChild(obj,child,int) repeat wait(int); until (obj:FindFirstChild(child)) end; |
|
|
| Report Abuse |
|
|
BenBonez
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 19362 |
|
|
| 19 Jul 2012 03:25 AM |
@SN0X
why would I want to wait longer than needed? |
|
|
| Report Abuse |
|
|
SN0X
|
  |
| Joined: 24 Oct 2011 |
| Total Posts: 7277 |
|
|
| 19 Jul 2012 03:28 AM |
function WaitForChild(obj,child,int) while not (obj:FindFirstChild(child)) do wait(int); end; end; |
|
|
| Report Abuse |
|
|