Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use For loops?

Asked by 9 years ago

Can someone explain for loops to me detailed with an example because some scripts that I make might not work because I need a for loop or something.

0
Do you mean `for _,v in pairs() do` or `for i = 1,5 do`? ISellCows 2 — 9y
0
@ISellCows Both FederalJustice 35 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

If you mean for _,v in pairs() do then here:

for _,v in pairs(game.Workspace:GetChildren()) do -- Gathers all the children in one go
    print(v.Name) -- It will print the name of every item in Workspace
end -- End the for _,v in pairs part

If you mean for i = 1,5 do then here:

local Children = game.Workspace:GetChildren() -- Gather all the children of Workspace
for i = 1,#Children do -- Repeats the loop until there is no more children in Workspace
    wait(.30) -- Adds a delay, if you didn't want a delay then you could just use the for _,v in pairs()
    print(Children[i].Name) -- Prints the name
end -- Ends the for i = 1,#Children part    

Sorry I can't really explain it any better, if you have any questions just comment and I'll try to help.

0
@ISellCows So the first for can have a wait in the code part or no, and also does the first one also mean for i,v in pairs do. Next, the second one what does [i] do? and what does the # in front of the Children do? And what will it print? FederalJustice 35 — 9y
0
KingLoneCat explains it better then I can in his answer ISellCows 2 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

For loops basically are loops that are used condition wise especially if you want to use increments in a loop....

for _, s in pairs(game.Workspace:GetChildren()) do

That above is not really a for loop in Roblox Lua they call that just simply Pairs... A for loop would be a loop that you run a set of number of times.... You can decide the increments you want to run it in such as if I want to change a Part transparency from 0 to 1 very gradually.... I would do something like this....

for i = 0, 1, .01 do       --[[The First number is the number that the for loop will start from and the Second number is where the for loop will end when it reaches that number and then the third number are the increments so it will keep adding .01 till it reaches 1 from 0 until it reaches 1 from 0 the for loop won't end.--]]
wait() --[[WHENEVER, You use loops make sure you put a wait() inside of the loop or else your studio will crash and you will lose your data unless you have saved it already or have an auto saved file.... --]]
game.Workspace.Part.Transparency = i --[[ So here the "i" is basically a variable that equals 0 at the beginning but, everytime the loop runs .01 is getting added to "i" since the "i" is what the variable you used for the loop...--]]
end

That's about the best way I myself can explain for loops, Sorry if this was helpless and if some of the vocabulary I used was beyond you I am sorry just try and comprehend it with Google Dictionary or ask someone what it means...

Thank you for reading my answer!

Answer this question