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: For and While?

Previous Thread :: Next Thread 
Flosky is not online. Flosky
Joined: 15 May 2011
Total Posts: 1815
21 Aug 2013 08:07 PM
I understand that using for is a type of loop, but I don't yet understand what it's for. If I wanted a loop I'd use while do. Can someone please explain to me the difference??
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Aug 2013 08:11 PM
for i = 1, 5 do
print(i)
end

>>1
>>2
>>3
>>4
>>5
Report Abuse
Azarth is not online. Azarth
Joined: 17 Aug 2012
Total Posts: 2760
21 Aug 2013 08:12 PM
while true do repeats a loop forever

When using a for loop you can control how many times something is ran.

Example for loop: This will print 1-5

for i = 1, 5 do
print(i)
wait()
end
---------
Most commonly you'd see a for loop like below, where i is the position in a table, v is the variable and the for loop puts all the players in a table. It runs once for each player, yet gets to all of them then stops when done.

for i,v in pairs(game.Players:GetPlayers()) do
print(v.Name) --Will print a list of all players in the game.
wait()
end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Aug 2013 08:14 PM
"while true do repeats a loop forever"
Not true, they can break the loop.
Also, it's not only while true.

while Game.Players:FindFirstChild("cntkillme") do
will repeat a loop until I'm not in game
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 08:14 PM
A for loop can be numeric or generic.

A numeric for loop will loop between start and end with a set increment. Once the variable supplied is equal to or greater than the end it will break the loop.
A generic for loop takes a function (normally an iterative function) and loops until the function returns nil
A while loop runs until its condition is true

Loops:

for i = 1, 5, 1/5 do --Numeric For

for i, v in next, workspace:GetChildren() do --Generic For

while a == b and b < 4 do --While-do
Report Abuse
Azarth is not online. Azarth
Joined: 17 Aug 2012
Total Posts: 2760
21 Aug 2013 08:16 PM
@cntkillme I know. I was trying to explain it in the simplest terms I could without including other conditions and making it more complicated for him to understand.
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
21 Aug 2013 08:18 PM
When explaining while-do loops, never use "while true do" as your example.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Aug 2013 08:20 PM
I NINJA'D SOMEONE FOR ONCE C:
Report Abuse
Ludici is not online. Ludici
Joined: 22 Jul 2013
Total Posts: 640
21 Aug 2013 10:39 PM
Okay I will try to explain this in the simplest way.
for loops:

for i = 1,5 do
print(i)
wait(1)
end
^
the script will run normally, the first time it runs, i will equal to 1 then when it reaches its end it will run again but i will = 2 this time, and so on and on. This is useful for resizing meshes and much more.
For example:
for i = 1,20 do
script.Parent.Mesh = Vector3.new(i,i,i)
wait()
end

While loops, in the other hand, run forever. If you make a while loop in the beginning of the script the rest of the script will not run as the loop will keep repeating itself. There is a way to get around that(coroutines) but we are not getting into that.

while condition do
while true do
while 1+1 ==2 do

The condition is what needs to be true so the loop runs then afterwards everything will repeat itself. Make sure to add a wait, or else your studio will crash.

while true do
workspace.Part.Size = Vector3.new(workspace.Part.Size.x +1,workspace.Part.Size.y +1,workspace.Part.Size.z +1)
wait()
end

If you have any questions just message me.
Sorry if I did not make myself clear.

~Make the simple, amazing~
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Aug 2013 10:41 PM
"while true do
workspace.Part.Size = Vector3.new(workspace.Part.Size.x +1,workspace.Part.Size.y +1,workspace.Part.Size.z +1)
wait()
end"

BIG PART INCOMMING
Report Abuse
Ludici is not online. Ludici
Joined: 22 Jul 2013
Total Posts: 640
21 Aug 2013 10:50 PM
Yes xD And lag XD

~Make the simple, amazing~
Report Abuse
Flosky is not online. Flosky
Joined: 15 May 2011
Total Posts: 1815
23 Aug 2013 03:32 PM
:o Thanks everyone!
Get ready for some mega lag with that last while loop. XD
Report Abuse
Javelandry is not online. Javelandry
Joined: 23 Jul 2013
Total Posts: 668
23 Aug 2013 03:35 PM
[ Content Deleted ]
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
23 Aug 2013 05:28 PM
^ stop being wrong.
Report Abuse
chaoticregandpledge is not online. chaoticregandpledge
Joined: 04 Dec 2009
Total Posts: 2894
23 Aug 2013 05:45 PM
While loops continue running a chunk of code until a condition is set to false.

Example:

p = Instance.new("StringValue", Workspace)
p.Name = "Str"
game:GetService("Debris"):AddItem(p, 5)

while workspace:findFirstChild("str") do
print("The value is still there")
end
Report Abuse
SecantFuture is not online. SecantFuture
Joined: 21 Aug 2013
Total Posts: 63
23 Aug 2013 05:49 PM
    while (condition) do
        --Do stuff if Condition is true
    end
    
    for Loop = (Interval1),(Interval2),(Increment) do
        --Do stuff for Interval2 - Interval1
    end
Report Abuse
Ludici is not online. Ludici
Joined: 22 Jul 2013
Total Posts: 640
23 Aug 2013 06:56 PM
I think he gets it lol.

~Make the simple, amazing~
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