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

How for iv in pairs and getservice varaibales works?

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 ????????????

4 comments

0

What do you mean? for i,v in pairs(game:GetService("Players"):GetPlayers()) do ?

0

YEAH

0

Im talking about for i, v in pairs and getservice

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. 

1 answer

✓ Accepted answer

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!

1 comment

0

U are smart guy! I followed u on roblox and sent friend request! U deserve this!

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