game.Players V.S. game:GetService("Players") Whats the difference?

The title says it all. So basically what’s better when using a service? game:GetService(“SomeService”)? Or game.SomeService? Which one should I use?

14 Likes

GetService is mainly used for services that aren’t guaranteed to be instantiated, like Teams and InsertService. For Players, it is fine to not use it, but you may get one complaint from an error “Players is not a valid member of game” because they have the Players service renamed if someone who doesn’t know what they are doing uses your code. If it is private code and Players isn’t renamed, there is no difference.

30 Likes

I know very little vocabulary… what is “instantiated”?

2 Likes

“Instanciated” means an instance exists of an object (it exists in the game). Services like Workspace are guaranteed to be instantiated (guaranteed to exist) because that is where parts are stored, as well as Players since all games container at least 1 Player. Something like the InsertService isn’t used in every game, so it is only created/referenced when needed.

9 Likes

You should do it for only services that are not directly visible or accessible with dot notation like TweenService or DataStoreService and stuff like that. But with Players, Workspace, ServerStorage, and everything else you can see in studio visibly, you can use dot notation. I’m not sure about the speed, but maybe you can check using tick()

5 Likes

There are a few reasons people use GetService,

  • Names of services aren’t the same as the ClassName (ex: UserInputService)

  • Not all services are already created (ex: Teams)

This shouldn’t be an issue when accessing services, considering you should only need to get them once.

But anyways, I did a test with a ton of methods on how to get ServerStorage.

Code
local t = tick()
for i=1,100000 do
	local s_storage = game.ServerStorage
end
print("index",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:GetService"ServerStorage"
end
print("GetService",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:getService"ServerStorage"
end
print("getService",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:FindService"ServerStorage"
end
print("FindService",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:findService"ServerStorage"
end
print("findService",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:service"ServerStorage"
end
print("service",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:FindFirstChild"ServerStorage"
end
print("FindFirstChild",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:findFirstChild"ServerStorage"
end
print("findFirstChild",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:FindFirstChildWhichIsA"ServerStorage"
end
print("FindFirstChildWhichIsA",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:findFirstChildWhichIsA"ServerStorage"
end
print("findFirstChildWhichIsA",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:FindFirstChildOfClass"ServerStorage"
end
print("FindFirstChildOfClass",tick()-t)
local t = tick()
for i=1,100000 do
	local s_storage = game:findFirstChildOfClass"ServerStorage"
end
print("findFirstChildOfClass",tick()-t)

And the results

  index 0.076725721359253
  GetService 0.054219484329224
  getService 0.054208040237427
  FindService 0.041592121124268
  findService 0.054868221282959
  service 0.055800437927246
  FindFirstChild 0.082368850708008
  findFirstChild 0.08403754234314
  FindFirstChildWhichIsA 0.32629704475403
  findFirstChildWhichIsA 0.35951209068298
  FindFirstChildOfClass 0.16426634788513
  findFirstChildOfClass 0.19518733024597

From the results, it appears that FindService is the best method, but it still suffers from the issue of services not existing, although it won’t throw an error immediately.

10 Likes

Just want to put it out there that GetService is the canonical way to retrieve a service, regardless of the whole renaming stuff. For the sake of staying consistent with your conventions, I’d also strongly suggest using GetService. Implicitly instantiated or not, I personally don’t ever use dot syntax for services.

@SupaAI (cc @Halalaluyafail3)

It’d be negligible. Not worth concerning yourself with.

5 Likes

I do the same xD. I just wanted to clarify why :GetService was even a thing if we have dot syntaxes.

1 Like

I get you. Dot syntax is pretty much the same as indexing any other asset, so by nature it’s logical that it would apply for services as well (children of the DataModel). GetService is there when services are weirdly named (which is a lot of them), since it searches the DataModel by instance ClassName.

2 Likes

Just wanted to add up something, all though my memory might fail me because I did actually try this like not a long time ago. Even though workspace, Players and many other services are objects parented to DataModel (game), they are actually a property of DataModel as well. So they are an object and a property at the same time, so changing the name of one of them won’t actually cause any problem because there is still the property which won’t change.

I remember testing this out but I’m probably wrong,
I unfortunately I can’t test it right now cuz I’m not currently on my computer

2 Likes

Looks like you’re correct.


(Ignore nil being printed that’s from one of my scripts in the place)

3 Likes

A follow up

So I checked everything out and I was kind of wrong. This does exist, but only applies for Worksapce

1 Like

Yeah! But this is the case only for Workspace it doesn’t work with other services

1 Like

5 Likes