The DevHub page about GetService() mentions that If the service does not yet exist it will be created and the new service is returned. But if this creates a new service, can I assign a function to it? Or an event?
GetService initializes the service if it isn’t already. For example, open the “Classic Baseplate” template, and assuming a plugin hasn’t called GetService already, run
print(game:GetService("Teams"))
In the command bar. You should notice the Teams service pop up in the explorer. It hadn’t been initialized yet, so it wasn’t being displayed. If you had used game.Teams instead, it would have errored with “Teams is not a valid member of DataModel”. If you try to pass the name of a service that doesn’t exist, it’ll just say it’s not a valid service name.
3 Likes