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