I just want to know
Is there a better version of For I, v loops
What I meant here is like wait() to task.wait()
I want to know is there a better version
I don’t really think that there is a “better” version.
There is table.foreach()
but from what I know it’s quite a bit slower than just a plain old for loop, and also a deprecated function used when generic for loops did not exist. So in short, you shouldn’t use it.
Here’s the documentation anyways though:
The only way to iterate through a table is by using a for loop, or to iterate over members of an Instance by using :GetChildren().
There is no “better” way of iterating through a table, or members of an Instance.
local myTable = {"Apples", "Oranges", "Grapes"}
for index, value in pairs(myTable) do
print(index, value)
end
local myModel = workspace.Model
for index, value in pairs(myModel:GetChildren()) do
print(index, value)
end
The simple answer is no. There is no known better version of for i, v loops. The only other for loop that you can use is for i = 1, (number that you want) loop. Here’s an example of one.
local Object = script.Parent
for i = 1, 350 do
Object.Position = Object.Position+Vector3.new(3, 0.2, 0.025)
task.wait()
end
Another could be look like this.
local Objects = script.Parent:GetChildren() -- This can be GetDescendants() too depending on if the parts children have children, if you know what I mean lol.
for i = 1, #Objects do
if Objects[i].ClassName == "Part" then
local Parts = Objects[i]
Parts.Color = Color3.fromRGB(200, 0, 200)
end
end
task.wait(1)
for i = 1, #Objects do
if Objects[i].ClassName == "Part" then
local Parts = Objects[i]
Parts.CFrame = CFrame.new(Parts.Position, game.Workspace.Bullseye)
task.wait()
end
end
The pairs and ipairs loop are probably the fastest there is.
and If i remember the code for pairs is
function pairs(tab)
function NextItem(lastind)
local newind=next(tab,lastind)
return newind,table[newind]
end
return NextItem
end
unless you can make this faster without screwing it up.