Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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!

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.