Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I see alot of LocalPlayer after a variable. What does that mean?

Asked by 4 years ago

When I watch some scripting videos, then I see alot when it says local Player = game.Players.LocalPlayer I know what the rest means. But I'm wondering what LocalPlayer is. What is it? Why is it useful? Etc.

Thank you,

Ducksneedhelp

0
The LocalPlayer is a pointer the the Client's affiliated Player Object; the user that your device is controlling. This variable is only accessible via LocalScripts, as these programs run on the connected user's machine—meaning it can cross-reference the Player Object to the personal device of the user. This is not functional in Scripts as these instances run on the operational Server. Ziffixture 6913 — 4y
0
Servers are designed to host & manage a multitude of Clients—connected users—it has no active Player Object to reference to, as it isn't playing the game, which is quite logical. Ziffixture 6913 — 4y

1 answer

Log in to vote
1
Answered by
nachsor 36
4 years ago

Simple answer, local player is the player.

Long answer...

Games have "2-sides", client and server. The client would be the player, while the server would be... well, the server. Now, what LocalPlayer does is it gets the current player. Since the server contains everyone's player, it does not know which one to take, thus returning nil. Although, when LocalPlayer is used on the client-side, in a LocalScript, the client only has 1 "player"(In quotes because other players can exist but they are not controlled by the same user.), so LocalPlayer would return that player. This essentially works the same way as say...

game.Players.PlayerAdded:Connect(function(player)
    --blah blah blah
end)

In this function, we create a variable called player, that variable is the player that just joined. This function here would go inside of server and would run anytime a player joins the server(only for the player or for the entire server depending on what you have inside).

TL;DR; LocalPlayer returns the player used by the client

0
It isn't confused of what Player to return, the LocalPlayer correlates to the device's affiliated Player Object. The Server is a machine that hosts the Clients, it isn't counted as one itself and that is pretty obvious... the device manages the game, it doesn't play it, therefore there is no Player Object of it's own, resulting in a failure to reference one. Ziffixture 6913 — 4y
Ad

Answer this question