dmjoe
|
  |
| Joined: 01 May 2009 |
| Total Posts: 2387 |
|
|
| 20 Mar 2013 07:46 AM |
Is there a page in the wiki that explains things like this?
By looking at other scripts, and on scripting helpers.. I've derived this
value = game.Child:GetChildren() --Child being anything... Workspace, Lighting, Players, Teams, :GetService("AnyServiceName") for i = 1,#value do --I realy don't understand this line. value[i].DoStuff --I understand that this takes the [i] or all items that were found in the last line. end --ending the for do function
Thanks!
~print("pew")~ |
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 20 Mar 2013 07:57 AM |
for i = 1,#value do
The for loop runs the code within it's scope a specified amount of times. The syntax of a for loop is this
for start, end, increment do
end
The increment is automatically one, so you don't have to set that. The 'i' is simply a variable that stores what iteration the for loop is on.
for i = 1,#value do
'value' is a table containing the children of an object, for example {part, player, part2, etc}. By using a hashtag you can get the AMOUNT of objects in that table. So if value had 10 objects in and you did for i = 1,#value do, the for loop would run 10 times.
value[i].DoStuff
Square brackets after a table allow you to access the corresponding object in the table. For example
value = {"Hi", "How", "Are", "You"}
print(value[1]..value[2]..value[3]..value[4])
would show in the output.
> HiHowAreYou
Now if the object in the table is a part, or whatever, you can access the properties like your normally would. So if you had a table that contains all the children of game.Players, you could set all their healths to 0, for example:
players = game.Players:GetChildren() for i=1, #players do players[i].Character.Humanoid.Health =0 end
Hope that clears things up for ya.
|
|
|
| Report Abuse |
|
|
dmjoe
|
  |
| Joined: 01 May 2009 |
| Total Posts: 2387 |
|
|
| 20 Mar 2013 08:06 AM |
Thank you! This will be useful.
Assuming I want it to run once, all I REALLY need (if 1 is by default is)
for i, #value do No?
Also, I have seen something like this this in the past: for i=_, #players do (Although I don't think that is what the code looked like, I'm sure you know what I mean) What does this mean?
Thanks again!
~print("pew")~ |
|
|
| Report Abuse |
|
|
As8D
|
  |
| Joined: 24 Dec 2009 |
| Total Posts: 2907 |
|
|
| 20 Mar 2013 08:06 AM |
Hi Joe.
for variable = startnumber, endnumber [, interval] do The line above will loop until variable + interval has reached endnumber. The default value for interval is 1, but if you ex. do something like: for value = 5, 0, -0.065 do then the final "value" variable won't match 0.
There's different kinds of for loops, but similar is that their syntax stuff is somewhat similar, as I believe that, like with the "next()" function, a for loop will continue until nil is returned.
Yeah, let's move on to for value1, value2, ... in loopFunction do Ok, so you usually find this kind of loop when using tables. Or childrens. It's more unbreakable, as, if you used "for value = 1, #parent:GetChildren() do", then the script would break, in case #parent:GetChildren() is smaller than 1 (Zero!)
Well, here's some literature sources: http://wiki.roblox.com/index.php/Function_Dump/Core_Functions#next http://wiki.roblox.com/index.php/Loops http://www.lua.org/pil/4.3.4.html http://wiki.roblox.com/index.php/Generic_for
- As, I don't hope someone posted an answer before me. D: |
|
|
| Report Abuse |
|
|
dmjoe
|
  |
| Joined: 01 May 2009 |
| Total Posts: 2387 |
|
|
| 20 Mar 2013 08:10 AM |
Hi As!
Thank you for the explanation, as well as the resources; I will take a look at these. As I've been advancing in RBX.Lua, this has been something pulling me back.
- Joe, Someone did post an answer before you. I love you all the same :D |
|
|
| Report Abuse |
|
|
|
| 20 Mar 2013 11:21 AM |
I heart your comments so will expand upon them (I may be wrong, though. Assume NOTHING!):
value = game.Child:GetChildren() --Child being anything... Workspace, Lighting, Players. Creats a Table of all these items, which are in the folder "game", lableing them with a number as well, starting with 1.
local NumberOfThingsWeFound = #value -- # seems to be a Command/Function? which returns the Total number of items in a Table. Look it up in the Wiki.
for i = 1,#value do --I realy don't understand this line. Computer, I want you to do the same thing X number of times; specifically the same number of times as the number items I havbe in a Table, which you will find in your list of Variables. called "value". Get the total items; Stop when you reach that number. Start at 1. Use the variable i to keep track of which the X, we are currntly at. Let's say you found 1000 Parts & Models in your Workspace....
value[i].DoStuff --I understand that this takes the [i] or all items that were found in the last line. -- I have no idea what DoStuff is, but i will equal 1 the first time thru this loop, so the first time thru value(i) will equal the first item in the table u made.
end --ending the for do function -- This is the end of a "Block". We look for the first, previouse "do" to see where this block starts. Since it is a For..do. we go back to the For statement from here; Increase i by 1; check if i > #value. If it is greater than NumberOfThingsWeFound then we are done; come back here; forget i ever existed, and continue on with whatever is unde this end. |
|
|
| Report Abuse |
|
|
As8D
|
  |
| Joined: 24 Dec 2009 |
| Total Posts: 2907 |
|
|
| 20 Mar 2013 11:34 AM |
Hm, since people ignore my "Forumer Of The Year" post, I shall feed you with even more information:
An exercise!
Let's try use our two for loops in the following case: We have this table...
local test = {honk = "2", [1338] = -1, {} = 0, "E4"}
so, we want to convert the values in the table into numbers and add them to each other. Since the # ... was it function you said? ^ is a replacement of table.getn, it will only get the number of sorted keys. Like in a table where index 1, 2 and 3 is found, the # will return 3. If the table is like the one above... it will return either 0 or 1, I cannot remember.
--------------------------------------------------------------- Test 1 - for value = start, end [, interval] do (rV = randomVariable).
local result = 0 for rV = 1, #test do result = result + (tonumber(test[rV]) or tonumber(test[rV], 16)) end
print(result) --------------------------------------------------------------- Test 2 - for key, value in pairs(table) do (The pairs function is used here).
local result = 0 for key, value in pairs(test) do result = result + (tonumber(value) or tonumber(value, 16)) end
print(result) ---------------------------------------------------------------
-As you can see, two completely different results. By the way, where did my ziggy go? |
|
|
| Report Abuse |
|
|
As8D
|
  |
| Joined: 24 Dec 2009 |
| Total Posts: 2907 |
|
|
| 20 Mar 2013 11:37 AM |
I made a mistake, it should be [{}], not simply {}. Puh.
^ Duplicate My Ziggy. -As |
|
|
| Report Abuse |
|
|
dmjoe
|
  |
| Joined: 01 May 2009 |
| Total Posts: 2387 |
|
|
| 22 Mar 2013 09:57 AM |
You guys rock :D
However.. "in pairs" How is it different to this? How is it used?
~print("pew")~ |
|
|
| Report Abuse |
|
|
Fedorakid
|
  |
| Joined: 17 Jul 2010 |
| Total Posts: 7079 |
|
|
| 22 Mar 2013 10:09 AM |
for index,value pairs(game.Workspace:GetChildren()) do value:Destroy() end
Index and value is both variables and you put in what to get inside the pairs. |
|
|
| Report Abuse |
|
|
|
| 22 Mar 2013 10:14 AM |
I only use this method of iteration:
for i, var in pairs(table) do end |
|
|
| Report Abuse |
|
|
adark
|
  |
| Joined: 13 Jan 2008 |
| Total Posts: 6412 |
|
|
| 22 Mar 2013 10:51 AM |
| http://wiki.roblox.com/index.php/Loops |
|
|
| Report Abuse |
|
|
As8D
|
  |
| Joined: 24 Dec 2009 |
| Total Posts: 2907 |
|
|
| 22 Mar 2013 11:31 AM |
@dmjoe...
Uhm, related to what?
pairs is, as my references may have told you, a function that will return the index of a table and call the next method. Then the loop will continue until pairs return nil.
I have not tried making any iterations myself, but I guess something like this would be a fine example to prove my theory: -------------------------------------------------------------- local begin = 0
function iterator() if begin < 5 then begin = begin + 1 return begin end end
for variable in iterator do print(variable) end -------------------------------------------------------------- I don't know if the script will work, but it's supposed to print the numbers from 1 to 5.
- As, haven't checked up on my HTML knowledge for some time. My brain is beginning to rust. |
|
|
| Report Abuse |
|
|