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

What can 'for i, v in next, list do' be used for?

Hello everyone, I recently came across the for i, v in next variant of the for loop and was wondering if its only use was to loop through a list? After experimenting, I found it worked similarly, if not the same as for i, v in pairs? If not, what can it be used for that pairs cannot?

local example = {"apple", "banana" ,"carrot", "burger"}

for i, v in next, example do
	print(v)
end

Output from pairs; apple, banana, carrot, burger

Output from next; apple, banana, carrot, burger

1 answer

✓ Accepted answer

It just cycles through a table giving its index and its value. For example this is useful for verification or other things to do with verifying.

Edit: I think their the same thing. I use next for everything, cause that's just me.

Example 1:

local AllowedPlayers = {"BradNewTypical", "Utter_Incompetence"}

for i,v in next, AllowedPlayers do
	if game.Players.LocalPlayer.Name == v then
		print("Allowed")
	end
end

Example 2:

local Ranks = {
	["owner"] = "Utter_Incompetence",
	["admin"] = "BradNewTypical"
}

for i,v in next, Ranks do
	if game.Players.LocalPlayer.Name == v then
		if i == "owner" then
			print("Hey it's an owner")
		elseif i == "admin" then
			print("Hey it's an admin")
		end
	end
end

Good for admin commands, I guess and other things.

1 comment

0

Alright, thanks for clarifying!

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