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

Why use services for things inside of the game?

Asked by 9 years ago

So I have lately been seeing a lot of people user Services to get things inside of the game I was wondering why its necessary an example of what I am talking about is something like this...

Local players = game:GetService("Players") 

Why do they use the one above?

Local players = game.Players

Why not use this one above? Does it matter is there a difference between those two? Isn't it getting the same thing? Please I would love it someone would tell me which one is more efficient and smoother to use programming wise.

0
I'm tempted to say it's because services might not be available right at the start of the game, but I have no proof to back that up. XAXA 1569 — 9y
2
It really depends on how the developer writes his code. Some like to take pride and write long useless code. rexbit 707 — 9y
0
Well if they are not available then why use them? KingLoneCat 2642 — 9y
0
good question koolkid8099 705 — 9y
0
Thank you. KingLoneCat 2642 — 9y

2 answers

Log in to vote
8
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Even though it's not a good idea, most services can be renamed.

That means that if you're writing code that you want other people to use, and you don't want it to get broken by a renamed service, you use :GetService, which searches by the service's classname instead of the name the developer put in Studio.

In this way, it's a little more robust. Usually you don't want lots of pieces of code depending on lots of minor configuration details. Though it's not necessarily a good idea to rename ServerScriptService to SSS, you'd probably not want that change to break everything.

0
Oh, so it's mostly used for others sake? So I don't have to do that to a script if I want to use it just for myself? KingLoneCat 2642 — 9y
1
No. As Blue said, it's to make sure that *your* scripts don't break whenever Roblox updates the names of their services. I don't think this has happened yet, however (the wiki provided the example of "RunService" being formerly called "Run Service", but I can't find proof for that.) XAXA 1569 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Some services don't exist inside game by default, and you can't create them via Instance.new. To create these services for your use, you call GetService. If the service doesn't exist, GetService creates it for you. because ROBLOX holds the right to make any service except for the Workspace act like this, you should use GetService wherever possible.

0
I know that you can use services to get things that are not inside of the game however, my question states why people use GetService for things inside of the game? KingLoneCat 2642 — 9y
0
Consistent syntax, mostly. You can use the direct form on some common ones, like Players, but I'd use GetService wherever possible just in case. It's good coding style. iconmaster 301 — 9y

Answer this question