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

How to properly use the numeric 'for' loop?

Asked by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

Hey! So, I rarely use the numeric for loop, so I'm having a bit of trouble. The script below is supposed to move a group of parts, 17.3 studs up, on their Y axis. It currently keeps going up, and doesn't stop, at least I can't see it stop. I'm pretty sure the first line could use some fixing up. All help is appreciated!

for i = 0, 17.3, .01 do
    for a,v in pairs(script.Parent:GetChildren()) do
        if not v:IsA("Script") then
            v.Position = v.Position + Vector3.new(0,.01,0)
            wait(.1)
        end
    end
end
0
It just takes a long time to finish because you're going up by .01, and waiting .1 seconds each time. At this rate, it would take 1730 seconds to finish. Remove the wait, or change it to wait() for it to take less time. dyler3 1510 — 9y
0
Sorry, 173 seconds, not 1730. dyler3 1510 — 9y

1 answer

Log in to vote
2
Answered by 9 years ago

Right before I explain, there is a WIKI Documentation about this specific topic. :P

How to properly use a For loop?

As the WIKI says, The for loop is a way of running a command or set of commands a set number of times., in other words, how many times to run it (Or, in other words (again), how many times it should run), as you'll sometimes see;

for i = 1, 5 do --Will loop 5 times
    print(i) --Prints variable 'i' (the iterator) each loop
end --Ends the chunk

There are multiple ways of using it, as such the code you are using, however, looking at the code, there are errors in it, not a lot, but some errors (not the type that break your code execution, but coding errors). Lets take a look at your code;

--KEEP IN MIND: This is your original code, I'll explain the errors later on
for i = 0, 17.3, .01 do
    for a,v in pairs(script.Parent:GetChildren()) do
        if not v:IsA("Script") then
            v.Position = v.Position + Vector3.new(0,.01,0)
            wait(.1)
        end
    end
end

There are multiple errors within it (that I can notice), allow me to point them out;

for i = 0, 17.3, .01 do --Fine, but, why '0.01'? That'll take a while to reach up to '17.3'
    for a,v in pairs(script.Parent:GetChildren()) do --No errors here! :D
        if not v:IsA("Script") then --Error - It is checking if the current Child is not a 'Script' Instance. You way be wondering, 'How is there an error here?' The error is that what if a Child that is not a 'Script' (Model, (Something)Value, ect.) was inside the Script's Parent as well? That will break execution.
            v.Position = v.Position + Vector3.new(0,.01,0) --Error - Position detects Collision (If a Nearby Part is where it's own Position is), that is the reason for your 'BasePart''s floating in different directions
            wait(.1) --Error - Why is the 'Wait' here? It will wait '0.1' seconds before looping to the next 'Child'
        end --Ends the chunk
    end --Ends the chunk
end --Ends the chunk

As I have pointed out, there are 2 errors in your code, however, these can be fixed by changing a bit of the code;

for i = 0, 17.3, .01 do
    for a,v in pairs(script.Parent:GetChildren()) do
        if v:IsA("BasePart") then --Aww, doesn't this look better? :) It will now check if the Current 'Child' being iterated through is a 'BasePart' Instance (WedgePart, Part, CornerWedgePart, ect.)
            v.CFrame = v.CFrame + Vector3.new(0,0.01,0)
        end
    end
    wait(.1) --This will wait '0.1' seconds before looping onto the next loop of the 'for' loop
end

Ahh, now doesn't your code look much better? :)

Information left out

In-case you don't know (Don't mean to make that sound rude :c ), a BasePart is a (I don't really know how to explain this, so I'll do as best I can! :D ) String (Used for the 'IsA' method) to specify a specific Part's type (Like the ClassName property of every Part, which tells it's Part type, 'IsA' does the same thing, but only it can use the 'BasePart' String (or function) to it's advantages to check a Child's Property) (Basically like it's it's own 'for' loop; it's like it loops though a 'Table' to determine which 'ClassName' property a Part's 'ClassName' is. Amazing, isn't it? :D ).

Hope this helped!

0
His code on line 5 is correct, TheAlphaStigme. Notice that he is adding to the existing position. If you add i, the position will increase exponentially in the Y direction. He's looking for a linear increase which is why he uses 0.01. AxeOfMen 434 — 9y
0
I see what you mean now, I didn't notice that before. ;-; Hang on, I'll edit my Answer. TheeDeathCaster 2368 — 9y
0
Well, I tried your script, and it still isn't moving the position correctly. Try reading my question again. One of the parts is a Union, and one is a normal part. The Union for some reason goes higher than the Part. Shawnyg 4330 — 9y
0
Oh, I know what's the cause of that. Can't believe I didn't notice that. Haha! Hang on, I'll edit my Answer again. TheeDeathCaster 2368 — 9y
Ad

Answer this question