Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is the difference between game:GetService("Workspace") and game.Workspace?

I have seen in other scripts people use game.Workspace, but doing :GetService() does the same. Is it supposed to be the same thing?

1 answer

✓ Accepted answer

GetService() ensures the existence of the Service you’re trying to load. If the Service does not exist this method if game will create it, making your script more robust. This is recommended as lots of Services can be renamed by programmers or malicious code that can make game.Service vulnerable.


Though, both game.Workspace and game:GetService("Workspace" aren’t needed. These pathways contain a constant reference to said Service, not only that, but there is a specialized keyword that automatically shortens both references. Simply call workspace

3 comments

0

Thanks but i have a question. What do you mean by "If the Service does not exist this method if game will create it"?

0

All available Services are within the explorer, they’re just invisible. If you go to File - Settings - Studio and scroll to the bottom, you should see an option to show all descendants of Game. You can see they’re no different from Objects, and they’re open to tampering. If a Service is accidentally deleted, or it’s name was Changed, the function will recognize the Service you’re trying to referen

0

Im still confused, what happens when the service doesn't exist?

This discussion is preserved as it was. New answers, comments, and votes are closed.