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
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!