PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

LocalPlayer

Players

Instance

LocalPlayer is a read-only property which contains a reference to the Player instance for which a LocalScript is running. For Scripts running on the server, this property is nil.

This property is useful when creating GUIs that display information about the player. You can use it to accessing the player’s Player/Character, PlayerGui, and Backpack.

Loading GUIs

When creating loading GUIs using ReplicatedFirst, sometimes a LocalScript may run before the Players/LocalPlayer is created and becomes available. In such cases, it is useful to yield until it is by using Instance/GetPropertyChangedSignal

local Players = game:GetService("Players")
local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer'):wait()

Note that this technique is not required for LocalScripts within in StarterGui, StarterPlayerScripts or StarterCharacterScripts, as these kinds of scripts only run if the Player object is already available.


Code Samples


This example demonstrates the Player/LoadCharacterBlocking function.

First, the script changes the local player’s appearance by changing the Player/CharacterAppearance property to the userId 772462. Then, the script fires the LoadCharacterBlocking function to reload the player’s character with the new appearance.

##Notes

  • This function cannot be used in scripts, but is usable in the command bar. As such, this example will only work as expected when fired from the command bar.
  • Since this function executes asynchronously, the script will not continue execution until the function finishes.
local player = game.Players.LocalPlayer

player.CharacterAppearanceId = 772462
player:LoadCharacterBlocking()

Setting the Player's Account Age

This example demonstrates how the Player/SetAccountAge function would be used if it was accessible. It sets the local player’s account age to 100 days.

​local player = game.Players.LocalPlayer
player:SetAccountAge(100)