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

Replacement for i,v in pairs? Is there any?

Asked by 3 years ago

I have found that to make my code efficient I used a lot of iv in pairs for image buttons but I use so much my code is filled with them most of the time. Is there any way I can get my code to be cleaner instead of using iv pairs or ipairs or any pairs. Any way I can use something like :GetChildren() and code my buttons using only like 1 line or any other functions you know of?

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

If you can narrow down what you're looking for rather than getting everything which is a child you could use:

OfClass

WhichIsA

See wiki pages for differences

FirstChild And pass it some criteria

You can also do:

local children = workspace.GetChildren()
local baseplate = children["baseplate"]

Or something similar if you need to wait for things to load or something :)

Using these you can miss out the loop if it only returns one specific item or at least reduce the number of loops!

For loops are really useful though so stop using them :) Hopefully this has been a little bit helpful, I'm sure others will have more to add though!

Ad
Log in to vote
0
Answered by 3 years ago

No. There isn't a replacement for it. In fact, using a for index, value loop is probably one of the best ways, and it takes less lines than directly applying those functions.

I remember reading a forum post which proved that a for i = 1, ... loop is the fastest. Although, it's essentially the same as a for i,v in ipairs loop. It's used like this:

local children = Instance:GetChildren()

for i = 1, #children do
    print(children[i].Name)
end

If you're looking for compactness, I suggest you look at module scripts.

Answer this question