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: Loops

Previous Thread :: Next Thread 
dockboy20006 is online. dockboy20006
Joined: 14 Feb 2011
Total Posts: 251
11 Jan 2017 05:52 PM
I am stuck on understanding loops... Help? (I am using wiki)
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
11 Jan 2017 05:56 PM
You should probably understand numerical for loops because they are by far the most simple.

General syntax is for var = initial, limit [, step = 1]
The [ ] imply it's optional and in this case step defaults to 1. A few examples should make it clear:

for counter = 1, 10, 2 do
print(counter)
end

will print 1, 3, 5, 7, and 9

the counter is incremented by `step` every iteration until it reaches `limit`.
Report Abuse
dockboy20006 is online. dockboy20006
Joined: 14 Feb 2011
Total Posts: 251
11 Jan 2017 06:03 PM
for i = 1, 5 do ---- What does the i have to do with anything? And why is the 1 there?
print 'Hello World' -- I understand that the '5' means prints 'Hello World' 5 times.
end

Report Abuse
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
11 Jan 2017 06:07 PM
i is the variable that holds the value of the loop

you can print(i) and it'll print the number that the loop is on


the 1 is the starting value, and the 5 is the maximum value that the i variable can reach
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
11 Jan 2017 06:10 PM
`i` is the name of the counter in that example
1 is the INITIAL value of the counter
5 is the LIMIT (meaning counter will not surpass this value)
Report Abuse
dockboy20006 is online. dockboy20006
Joined: 14 Feb 2011
Total Posts: 251
11 Jan 2017 06:14 PM
'i' is the one who is counting, '1' is where it starts counting, and '5' is where it stops?
Report Abuse
Nickbrownie is not online. Nickbrownie
Joined: 16 Mar 2014
Total Posts: 90
11 Jan 2017 06:16 PM
there are while loops and for loops while loops: while condition do wait() -- code to be repeated end does the same thing over and over until condition equals false for loops: for i, v, increment do wait() -- code to do repeatedly end if you wanted to print 1 - 10 using this you would do: for i = 1, 10, 1 do -- # # ###### to start with, v is how many times to do it, the last one is the increment +1, it can be omitted if it's one. wait() print(i) end the for loop i do the most is: for _, v in pairs(table) do wait() -- code end I use this when I need to do something for every item in a table, instead of pairs you can also do ipairs and next, but i only know how to do pairs. Message me if you need any more help. :P bai
Report Abuse
Nickbrownie is not online. Nickbrownie
Joined: 16 Mar 2014
Total Posts: 90
11 Jan 2017 06:17 PM
I did a really bad job explaining xP
Someone clean it up for me please xD
Report Abuse
dockboy20006 is online. dockboy20006
Joined: 14 Feb 2011
Total Posts: 251
11 Jan 2017 06:18 PM
Don't worry, I just plainly don't understand loops. Why would I need the '3 types of loops?'
Report Abuse
Nickbrownie is not online. Nickbrownie
Joined: 16 Mar 2014
Total Posts: 90
08 Feb 2017 06:44 PM
Message me and I could explain it to you on a team create game
Report Abuse
Equilibristic is not online. Equilibristic
Joined: 03 Sep 2010
Total Posts: 750
08 Feb 2017 06:58 PM
I'm gonna give you the concept of what a loop is.
A loop is something that tells a certain statement
to do this until something is true.

I think in lua it's not "do while" but "while do" or "while true do",
something like that.

Once you understand loops, it'll be fun.
Anyways, hope you learn it. :)




Report Abuse
Equilibristic is not online. Equilibristic
Joined: 03 Sep 2010
Total Posts: 750
08 Feb 2017 07:01 PM
Example of a while loop: (this example is not in lua, but you understand the concept probably.) int a = 1; while (a < 5); { cout << ####### ###### is.. " << a << endl; a = a + 1; } The "a = a + 1" part, the 1 is the incriment. You can make it a decrement if you change the (a < 5) to # ## ## 5). Increment - how much you would add to this variable Decrement - how much you would take from the variable. Sorry if I explained it roughly. Hope you understand.
Report Abuse
Equilibristic is not online. Equilibristic
Joined: 03 Sep 2010
Total Posts: 750
08 Feb 2017 07:02 PM
Ignore the hashtags, it's not needed in lua anyways, depends if you wanna display a message onto the output screen.


Report Abuse
DJ3SkullsEden is not online. DJ3SkullsEden
Joined: 06 Dec 2016
Total Posts: 182
08 Feb 2017 07:34 PM
for i = 1, 10 do --i is set to 1
print(i) --the value of i is shown in the output window
end --and it repeats, except i increases by 1 each time until it equals a value greater than 10.

This is like saying.

do
code
10 times

So i would be the number of times it is looping for, kinda.
i can be any type of starting point: it can is 5, it can be 1,000, it can be 1e-5.

for i = 2, 4 do
print(i)
end

This will show 2, 3 and 4 on the output window.

This is the generic for loop, or just a for loop.


For loops are used for code that can be redone multiple times or require a changing number that increases by 1 each time.

You can even change how much it increases by.

for i = 0, 100, 10 do
print(i)
end

This will show from 0x10 - 10x10 on the output window.

for i, v in pairs(t) do
print(i, v)
end

These are for in pairs loops, which require a table.

If you know about tables, then this may become easier to understand.

What this does is cycles through everything, from first to last, within the table named "t" and stores

1) the position of the value in 1
2) the value in v

You can do all sorts of things with these (and every other loop) and it allows you to cut down your code quite a bit.

I used these to check through parts to see if there is a certain part that shouldn't be there. If there is then it'll remove it.

for i, v in pairs(game.Workspace:GetChildren()) do
if v.BrickColor ~= BrickColor.new("Medium stone grey") then --The color I'm comparing it to is the default color
v:Destroy() --Removes the value not just from the table but from the game, ooo spooky
end
end

While loops are like if statements - they only run if what is inbetween the "if" and "then" is true.

if 1 == 1 then --will run, since 1 IS 1, so it is true
if true then --will run, since true is the value that you give when something is true or right
if game.Workspace.Part --will not run, it doesn't exist within the Workspace (well for me at least) so it will be "nil", which is also false
if false then --will not run, since false is the value that you give then something is false or wrong

Same thing applies to while loops.


CAUTION:

DO
NOT
FORGET
YOUR
WAIT()
WITHIN
YOUR
LOOPS
OTHERWISE
YOU
AND
EVEN
YOUR
PLAYERS
WILL

C
R
A
S
H
!
!
!
!
!
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
08 Feb 2017 07:38 PM
"DO
NOT
FORGET
YOUR
WAIT()
WITHIN
YOUR
LOOPS"

you completely contradicted yourself


Report Abuse
Nuclear_Energy is online. Nuclear_Energy
Joined: 21 Jul 2013
Total Posts: 1380
08 Feb 2017 07:42 PM
02-08-2017 05:34 PM
for i = 1, 10 do --i is set to 1
print(i) --the value of i is shown in the output window
end --and it repeats, except i increases by 1 each time until it equals a value greater than 10.

This is like saying.

do
code
10 times

So i would be the number of times it is looping for, kinda.
i can be any type of starting point: it can is 5, it can be 1,000, it can be 1e-5.

for i = 2, 4 do
print(i)
end

This will show 2, 3 and 4 on the output window.

This is the generic for loop, or just a for loop.


For loops are used for code that can be redone multiple times or require a changing number that increases by 1 each time.

You can even change how much it increases by.

for i = 0, 100, 10 do
print(i)
end

This will show from 0x10 - 10x10 on the output window.

for i, v in pairs(t) do
print(i, v)
end

These are for in pairs loops, which require a table.

If you know about tables, then this may become easier to understand.

What this does is cycles through everything, from first to last, within the table named "t" and stores

1) the position of the value in 1
2) the value in v

You can do all sorts of things with these (and every other loop) and it allows you to cut down your code quite a bit.

I used these to check through parts to see if there is a certain part that shouldn't be there. If there is then it'll remove it.

for i, v in pairs(game.Workspace:GetChildren()) do
if v.BrickColor ~= BrickColor.new("Medium stone grey") then --The color I'm comparing it to is the default color
v:Destroy() --Removes the value not just from the table but from the game, ooo spooky
end
end

While loops are like if statements - they only run if what is inbetween the "if" and "then" is true.

if 1 == 1 then --will run, since 1 IS 1, so it is true
if true then --will run, since true is the value that you give when something is true or right
if game.Workspace.Part --will not run, it doesn't exist within the Workspace (well for me at least) so it will be "nil", which is also false
if false then --will not run, since false is the value that you give then something is false or wrong

Same thing applies to while loops.


CAUTION:

DO
NOT
FORGET
YOUR
WAIT()
WITHIN
YOUR
LOOPS
OTHERWISE
YOU
AND
EVEN
YOUR
PLAYERS
WILL

C
R
A
S
H
!
!
!
!
02-08-2017 05:34 PM
for i = 1, 10 do --i is set to 1
print(i) --the value of i is shown in the output window
end --and it repeats, except i increases by 1 each time until it equals a value greater than 10.

This is like saying.

do
code
10 times

So i would be the number of times it is looping for, kinda.
i can be any type of starting point: it can is 5, it can be 1,000, it can be 1e-5.

for i = 2, 4 do
print(i)
end

This will show 2, 3 and 4 on the output window.

This is the generic for loop, or just a for loop.


For loops are used for code that can be redone multiple times or require a changing number that increases by 1 each time.

You can even change how much it increases by.

for i = 0, 100, 10 do
print(i)
end

This will show from 0x10 - 10x10 on the output window.

for i, v in pairs(t) do
print(i, v)
end

These are for in pairs loops, which require a table.

If you know about tables, then this may become easier to understand.

What this does is cycles through everything, from first to last, within the table named "t" and stores

1) the position of the value in 1
2) the value in v

You can do all sorts of things with these (and every other loop) and it allows you to cut down your code quite a bit.

I used these to check through parts to see if there is a certain part that shouldn't be there. If there is then it'll remove it.

for i, v in pairs(game.Workspace:GetChildren()) do
if v.BrickColor ~= BrickColor.new("Medium stone grey") then --The color I'm comparing it to is the default color
v:Destroy() --Removes the value not just from the table but from the game, ooo spooky
end
end

While loops are like if statements - they only run if what is inbetween the "if" and "then" is true.

if 1 == 1 then --will run, since 1 IS 1, so it is true
if true then --will run, since true is the value that you give when something is true or right
if game.Workspace.Part --will not run, it doesn't exist within the Workspace (well for me at least) so it will be "nil", which is also false
if false then --will not run, since false is the value that you give then something is false or wrong

Same thing applies to while loops.


CAUTION:

DO
NOT
FORGET
YOUR
WAIT()
WITHIN
YOUR
LOOPS
OTHERWISE
YOU
AND
EVEN
YOUR
PLAYERS
WILL

C
R
A
S
H
!
!
!
!
Report Abuse
DJ3SkullsEden is not online. DJ3SkullsEden
Joined: 06 Dec 2016
Total Posts: 182
08 Feb 2017 07:47 PM
How did I contradict myself? I only ever mentioned wait's at the end?

Oh well, if OP doesn't understand what I said then he can disregard my post as nonsensical gibberish.
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
08 Feb 2017 07:51 PM
"for i, v in pairs(t) do
print(i, v)
end"

I don't see a yield there


Report Abuse
DJ3SkullsEden is not online. DJ3SkullsEden
Joined: 06 Dec 2016
Total Posts: 182
08 Feb 2017 07:55 PM
Reason as to why I added the whole warning at the end. Because I didn't use any wait's.
Report Abuse
sircfenner is not online. sircfenner
Joined: 29 Aug 2008
Total Posts: 2430
08 Feb 2017 08:55 PM
That's not the reason you added the warning at the end. You added the warning because you wanted to warn people.

You simply forgot to follow your own advice. Fail.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Feb 2017 09:51 PM
"for i = 2, 4 do
print(i)
end
This will show 2, 3 and 4 on the output window.
This is the generic for loop, or just a for loop."

No it's not. Learn what a generic for loop is before trying to teach.
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
08 Feb 2017 10:17 PM
think of it like being like this

for i=0, 10, 1 do
print( i )
end


do --keeping it in the scope
local i = 0
local max = 10
local step = 1
while i < max do
print( i )
i = i + step
end
end
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
08 Feb 2017 10:18 PM
i <= max
Report Abuse
DJ3SkullsEden is not online. DJ3SkullsEden
Joined: 06 Dec 2016
Total Posts: 182
08 Feb 2017 11:36 PM
@sircfenner

Real reason: I rushed it and panicked.
Stop assuming, that's the problem with a small fraction of the Scripter's forum; you assume too much.
Report Abuse
DJ3SkullsEden is not online. DJ3SkullsEden
Joined: 06 Dec 2016
Total Posts: 182
08 Feb 2017 11:37 PM
@cntkillme

C h i l l

Was misinformed, my bad dude.
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