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

How for iv in pairs and getservice varaibales works?

Asked by 4 years ago

I firstly go to alvinblox for info but tutorial was toooooo long +I wanted to learn getservice too so i ask a question. Whats about that whole for iv in pairs and getservice things???? Pls write donw in comments ????????????

0
What do you mean? `for i,v in pairs(game:GetService("Players"):GetPlayers()) do` ? Leamir 3138 — 4y
0
YEAH JasiuJasiuB 13 — 4y
0
Im talking about for i, v in pairs and getservice JasiuJasiuB 13 — 4y
0
GetService ensures the existence of the Service you're receiving, and will create it if it doesn't exist. There are many more complications that arise with .Service, so I recommend you visit the Dev Wiki for more details.  Ziffixture 6913 — 4y

1 answer

Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Hello, JasiuJasiuB!

For ... do loop

a for ... do loop increments a value until it reaches an objective:

1for i=1, 5 do
2    print(i)
3end

prints:

11
22
33
44
55

for ... do loops can be also used with tables:

1local table = {"value1", "value2", "value3", "value4", "value5"}
2 
3for i,v in pairs (table) do
4    print(v)
5end

this results in:

1value1
2value2
3value3
4value4
5value5

It is exactaly the same as doing

1local table = {"value1", "value2", "value3", "value4", "value5"}
2 
3print(table[1])
4print(table[2])
5print(table[3])
6print(table[4])
7print(table[5])

This makes your code lots smaller, as you can see

game:GetService()

game:GetService() is used to get internal services of roblox, like the UserInputService or MessagingService

There're some special services, that you can see on the studio, such as Players, ReplicatedStorage, ServerStorage, and many others.

I'll explain just what you want, as I don't want this to be too long: game:GetService("Players"):GetPlayers() -- this will return a table containing all the players objects on the game, so we can use a for ... do loop to cycle it:

1local playersList = game:GetService("Players"):GetPlayers()
2for i,v in pairs(playersList) do
3    --code
4end

Useful links:

https://developer.roblox.com/en-us/api-reference/function/ServiceProvider/GetService

https://www.lua.org/pil/4.3.4.html

https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayers

Hope this helps!

0
U are smart guy! I followed u on roblox and sent friend request! U deserve this! JasiuJasiuB 13 — 4y
Ad

Answer this question