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 » Scripters
Home Search
 

Re: Putting a wait(1) in LocalScripts

Previous Thread :: Next Thread 
Seranok is online. 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
DamageGamerBoy is not online. DamageGamerBoy
Joined: 08 Jun 2010
Total Posts: 10133
18 Jul 2012 11:03 PM
haider
Report Abuse
LiteralException is not online. LiteralException
Joined: 10 Nov 2011
Total Posts: 23
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 is not online. 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 is not online. stravant
Forum Moderator
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
Candymaniac is not online. Candymaniac
Joined: 08 Oct 2009
Total Posts: 8985
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 is not online. 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 is not online. 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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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