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

Will for i,v in pairs be useful in basic rblx games?

I'm not sure if for i,v in pairs would be useful in a basic rblx game. Since I don't understand it much(dont explain to me what it is), i will try to understand it more in the future when games would be important to me. And if so, can you explain why and an example of how you can use it?

6 comments

0

ill tell u

0

umm okay?

0

wait a sec ok?

0

Ok watch this vid :P

1 answer

This is a special form of iteration designed to traverse through an array. The pairs() function will push the current index point, and element within the table at the respective point of iteration into two variables, i and v. These define Index and Value. These two variables can be named whatever you like, or ignored if not needed via using the special character _.

This is highly useful as it can allow us to perform multiple tasks on tones of Instances with barely any lines of code—or process a lot of information quickly—this is something you'll find yourself needing to do, or doing in several basic programs.

Let's take a look at a basic application, killing everyone in-game:

local Players = game:GetService("Players"):GetPlayers() --// Gives an array.

for _,Player in pairs(Players) do
	if (Player.Character) then
		local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
		if (Humanoid) then
			Humanoid:TakeDamage(Humanoid.Health)
		end
	end
end

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