Why does leaderstats not show up in my player? (player.leaderstats)

local plr = game:GetService("Players").LocalPlayer;

game:GetService('RunService').RenderStepped:Connect(function()
	script.Parent.Text = tostring(plr.leaderstats.Coins.Value)
end)

Place this in a localscript inside the TextLabel.

1 Like

It is in a text label and is a local script.

Then use

To get the player in a local script

Avoid using while wait() here. Store a cache for the leaderstats and time to time update it in the database. If the data does not exist, the value of Cash will be set to nil. So check if data exists.

Idk what you mean. Can you explain it simpler pls?

Basically, remove while wait() do loop to update coins.Value. Instead, whenever someone’s cash is to be increased, you change the value of Coins in the leaderstats using plr.leaderstats.Cash.Value = somevalue (assuming that plr is the player) and not the datastore directly. At some point, lets say when the player leaves, you store the value of Coins (in the leaderstats) to the datatstore. This is more systematic.

Also check if the data exists when using :Get() using pcalls

Do this:

leaderstats.Parent = plr

How do I check if Coins increases and decreases? Also, sometimes when I go into my player in game.Players in “Current: Client”, it doesn’t show the leaderstats folder. So, the local script in the text label will just keep printing errors.

He already parented leaderstats to the player

Although I strongly advise, to parent any instance after its properties are set.

If you could tell me if there something like a shop that requires you to spend your Coins or something similar, then as you update the value of Coins, use a remoteevent and then update the value of Coins on the server to the value you desire.

Okay, but leaderstats doesn’t show up all the time. Whenever I play, there’s a small chance that leaderstats won’t be in my player. That’s the problem.

local leaderstats = Instance.new('Folder')
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr

local coins = Instance.new('IntValue')
coins.Name = 'Coins'
coins.Value = 0
coins.Parent = leaderstats

Try this when creating a leaderstats folder.

Isn’t

local leaderstats = Instance.new(‘Folder’, plr)

the same as

local leaderstats = Instance.new(‘Folder’)
leaderstats.Parent = plr

Yes, But the reason is, try setting the Parent after setting the required properties of the instances. Reason being: You are using RunService to set the text. Before the leaderstats folder is actually created, the script runs and spams an error saying “leaderstats is not a valid member of plr”.

So put this in the script:

local plr = game:GetService("Players").LocalPlayer;

game:GetService('RunService').RenderStepped:Connect(function()
    if plr:FindFirstChild("leaderstats") then
	    script.Parent.Text = tostring(plr.leaderstats.Coins.Value)
    end
end)

That gets rid of the spamming error. But how am I gonna put leaderstats in the player if leaderstats isn’t there?

PlayerAdded event is fired when the player is added to “Players” so technically, there isnt a point where the player isn’t there (and at that point, you want to put leaderstats in the player).

Check my reply again, I edited it.

I am not sure what you mean by this.

If leaderstats wasn’t in the player, then how would I make a new leaderstats inside the player?

I already replied with the following code. :+1: