Hey, there might be some mixed opinions or rather ways to use FindService() and GetService() on Roblox for game development or simple modules.
Differences?
There are two functions called FindService() and GetService(), two services that have existed for sometime now and I always wanted to know and ask why these two exist and why chose one over the other, I’ve seen people use them in tutorials, modules, and scripts (GetService), but never seen someone use Find.
Documentation / API for Find and Get are a bit confusing as one uses printing when called using FindService() to print out the services’ name, and one is to get a service for game development and production:
Here is a mock-up script that took me a couple minutes of the two services:
local hello = game:FindService("ReplicatedStorage")
local hello1 = game:GetService("ReplicatedStorage")
local folder = Instance.new("Folder")
local folder1 = Instance.new("Folder")
function placeInstanceAtFind()
local t = tick()
local tt = {}
folder.Parent = hello
if folder.Parent == hello then
warn("Is a parent")
print(t)
else
error("Isn't a parent")
print(t)
end
end
function placeInstanceAtGet()
local t = tick()
local tt = {}
folder1.Parent = hello1
if folder1.Parent == hello1 then
warn("Is a parent")
print(t)
else
error("Isn't a parent")
print(t)
end
end
if pcall(placeInstanceAtGet) then
local t = time()
warn(t.." Good condition")
else
wait(10) do
error("pcall() found the condition to be wrong")
end
end
if pcall(placeInstanceAtFind) then
local t = time()
warn(t.." Good condition")
else
wait(10) do
error("pcall() found the condition to be wrong")
end
end
placeInstanceAtFind()
placeInstanceAtGet()
Testing this gave the output “I” should have expected since both of them do the same behavior of collecting a service in the Explorer or Roblox Engine that is a ServiceProvider:
Looking back at the documentation and script I’ve created, I see that they have the same pcall that is expected, and that it wouldn’t error (obviously, just for checking)
I could consider making a module of this for better calling and detecting of FindService() and GetService() all together like a call() or pcall() wrap in some development game.
Question for all:
A question that I have to answer: Is there a clear difference or similarity between FindService() and GetService(), should one or the other be used in production, something I’m missing?
You can easily find out using the developer hub, but I did for you.
It says on the page for GetService “If the service does not yet exist it will be created and the new service is returned.”, while on the page for FindService it says “Returns the service specified by the given className if it’s already created, errors for an invalid name.”.
Seems to me that GetService gets the service whether it has been created or not, while FindService only finds pre-existing services.
Personally, I just use GetService because it’s the simplest answer.
> local S =os.clock()for i=1,1000000 do game:GetService("Players").Archivable = true end print(os.clock()-S)
0.54912580002565
> local S =os.clock()local p=game:GetService("Players") for i=1,1000000 do game:FindService("Players").Archivable = true end print(os.clock()-S)
0.42678650002927
Not always
I’d assume it was made so people doesn’t have to keep using :GetService or use a module that contains all of them.
It will create the service if the service doesn’t exist when calling :GetService(Service).
Returns a service with the class name requested. When called with the name of a service (such as Debris ) it will return the instance of that service. If the service does not yet exist it will be created and the new service is returned. This is the only way to create some services, and can also be used for services that have unusual names, e.g. RunService’s name is “Run Service”.
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.
As it has been stated in this thread, :FindService will only return the service if it exists, although :GetService does the same thing, it will create the service if it doesn’t already exist. Normally there are a few services that are already existing on run which may be retrieved by :FindService so I made a list of:
Keep in mind that some services automatically start after a short delay such as GroupService.
It’s still recommended to call :GetService just to be safe as it doesn’t have any noticeable performance losses in comparison to :FindService. Also, a few internal services are included in this list which should be ignored due to their lack of use to devs.
If the service is not already there, like DataStoreService, it will insert the service into the DataModel. I personally ALWAYS use GetService because it’s shorter than FindService.