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:

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

prints:

1
2
3
4
5

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

local table = {"value1", "value2", "value3", "value4", "value5"}

for i,v in pairs (table) do
    print(v)
end

this results in:

value1
value2
value3
value4
value5

It is exactaly the same as doing

local table = {"value1", "value2", "value3", "value4", "value5"}

print(table[1])
print(table[2])
print(table[3])
print(table[4])
print(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:

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

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