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

How do i use for loops ( in pairs() ) ?? And why do you need to use it?

Asked by
qVoided 221 Moderation Voter
4 years ago

I only know this script:

***local Table = {roblox = "roblox", minecraft = "minecraft"}

for i,v in pairs(Table) do print(i,v) end***

i don't know why you would use something like this, but i know this is useful for timers:


for i = 5,0,-1 do print(i) wait(1) end


Please answer if you know

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

It's really useful if you want to do something to a select group of objects. Let's say you're creating a fighting game and you want every player in team red to be rewarded with a couple of coins when red wins and same goes for team blue.

Then you could create something like this to easily accomplish this.

function redWin()
    for i,v in pairs(game.Players:GetChildren()) do --main loop
        if v.Team == "red" then
            v.Coins = v.Coins + 50 --reward player
        end
    end
end

function blueWin()
    for i,v in pairs(game.Players:GetChildren()) do --main loop
        if v.Team == "blue" then
            v.Coins = v.Coins + 50 --reward player
        end
    end
end
0
u can use team:GetPlayers() instead of Players:GetPlayers()/Players:GetChildren() Mr_Unlucky 1085 — 4y
0
I was just thinking if you used value based team systems (which i use, it also seems more common nowadays) Vinceberget 1420 — 4y
Ad

Answer this question