These Strings do the same thing?

Hi developers,
I was producing a script, and when I went to look at some tutorials on the internet, I came across a method different from mine …

some people are using:
local player = game:GetService("Players").LocalPlayer

and i am using:
local player = game.Players.LocalPlayer

I didn’t see any difference in the functioning of my script,
what is the biggest difference between them?

Typically people opt to use game:GetService("Players") as to ensure you get the Service whereas game.Players just gets the first thing it finds called Players, which could break in the event it is not what you expect.

There’s no other difference besides that I believe, it is more recommended to use game:GetService() for obtaining services to ensure you get the service that you want

2 Likes

These are practically the same. In my opinion you should always use the second one, it’s shorter and neater. The only difference is the first one is safer, this is because :GetService() does a ClassName lookup, and doesn’t give back a result depending on the Name. What this means is if someone (you or a malicious client) changed the name of a service on his end, the game.Service way will error (this is since services are actually a child of game, with Workspace being an exception being a property).

For things only visible to the server, this is not a problem since the only person that could change the name is you, on the client, who cares! If an exploiter changes the name they will get errors, no one’s affected.

1 Like

really ty, I will try to learn more about it

1 Like

tyyy
I will learn more about it, thank you for your concern.

1 Like

You should absolutely not use the second one, since you need GetService in some places and it’s just consistency. Lots of services are not named the same their class name; try printing game.Instance.ClassName or looping through the DataModel’s children. On top of that, if you’re writing open source code, people are fully able to rename services in their game to whatever they want. game.Players might work in your places, but others might have it named players, p, or something else. GetService creates services that don’t exist yet, too: see Teams. (Exploiters can also locally rename services and break your game if you use the second method, but them breaking their own client is less of a concern than the other reasons for using it. I’m sure they have easier ways to stop LocalScript execution.)

2 Likes

wow, I understand what you mean, thanks for the feedback

In the second case you are calling the DataModel child Instance. In the other way you are calling directly the service which means : if you change the Players’ name instance you will access into that service by using game.newServiceName ( in that case the new name of the Instance) but always game:GetService("Players") . If you want to secure (very few) your game changing some services’ names ,using game:GetService() you won’t have the problem of changing the name of the service call. :grinning:

game.Players.Name = "banana"
-- now to call Players service you need to use the Name of the Instance
game.banana --now is players
-- with GetService() function you don'y have to worry about instance name so you can always call it with :
game:GetService("Players")
1 Like

This is not security. Exploiters are just as capable of using GetService/FindService as you are.

2 Likes

omG, so thankfull, really ty, ty for feedback

1 Like

Most exploiters are dumb and it was an example. I said :

very few :grinning:

1 Like

Pollo, exploiters can access basically anything you can with a local script with some additions and deductions. This is not for the sake of security whatsoever.

The only difference is the way it finds the ‘players’.

Players = game.Players - Directly try’s to access the child ‘Players’ (Basically a folder)

Players = game:GetService(“Players”) - Gets the Player folder through the service rather than trying direct access. Will find even in the case of changes to game.Players e.g changing parent or name.

The only relevance exploitation has to this is if you are getting it locally and an exploiter has changed the location or name of players. This is basically never done by exploiters as it will make the game unplayable for them which will remove the point of them exploiting in the first place. This means the only people this would effect are those exploiting, so I will repeat what everyone else has been saying. It simply doesn’t matter, only game:GetService(“Players”) accounts for changes to game.Players.

Edit:
Please refer to the API as it has a lot of information on pretty much everything. It is a useful dev tool.

1 Like

I don’t know how many times I thanked , but lol
TYY

That do not matter. And if exploiters were dumb, they would not even know exploit. The exploiters can’t see any ServerScript on ServerStorage or ServerScriptService, that how to secure a script.

1 Like

jesus I’ve just done an example . I also said very few.

1 Like

Well, if you look over the source code of some popular games that have open sourced like Prison Life (with good scripting and UI design), they always use GetService() and WaitForChild(). game.Service, in my opinion, is very unneat, because anything could happen to the service, it could get renamed, it could not be loaded yet etc.

2 Likes

Here are the both definition in a LocalScript for it:

  • game.Players.LocalPlayer
    Default: if Players get renamed to plr with example, it’s will throw an error.
  • game:GetService("Players").LocalPlayer
    Advantage: It’s get the service, which means it’s wont throw an error even if it’s renamed to plr by a exploiter.

So far, the most recommanded is:

game:GetService("Players").LocalPlayer

Hope this answer your question!

2 Likes

simple and direct, thanks for the feedback
(yes i will thank everyone who commented)