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

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

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

✓ Accepted answer

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

2 comments

0

u can use team:GetPlayers() instead of Players:GetPlayers()/Players:GetChildren()

0

I was just thinking if you used value based team systems (which i use, it also seems more common nowadays)

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