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

This Or That? It or It? game Or Game

Asked by 5 years ago

I'm new!! game.Players or game:GetService? Why???

0
I mean which is used to acess services and when.. StateSector 8 — 5y

1 answer

Log in to vote
4
Answered by 5 years ago

There is GetService and FindService.

People will use game.Players on the server side as this service will always exist and developers have no real good reason to change the same of this service from its default name.

If this name is changed then one of the two functions above will be needed to get "access" to this service. On the client side (local script) you will see GetService used often as an exploiter can change the names of services meaning that it can break how some local scripts work.

Lastly GetService is commonly used over FindService as GetService will create the service for that given name and return it, return the service if it exists or errors if there are no services in the api for this name.


A very common error I see in code is when people use GetService within an event that is used multiple times. This would require Roblox to check if there is a service and return it each time.

Example

someEvent.Changed:Connect(function()
    local dataStore = game:GetService("DataStoreService")
    -- other code
end)

-- reuse constant values 
local dataStore = game:GetService("DataStoreService")
someEvent.Changed:Connect(function()
    -- other code
end)

Hope this helps.

0
Thanks. Also sorry for the late response. StateSector 8 — 5y
0
After a little more reading. I get it! StateSector 8 — 5y
Ad

Answer this question