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

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

Asked by 4 years ago
Edited 4 years ago

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?

0
ill tell u Verbalase_notreal 23 — 4y
0
umm okay? legendcruncher82 -8 — 4y
0
wait a sec ok? Verbalase_notreal 23 — 4y
View all comments (2 more)

1 answer

Log in to vote
2
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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
Ad

Answer this question