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

I already posted this question on scripting helpers. I got some help, but didn’t get the answer I needed. Also, this is my first post on here.

I’m using Datastore 2 and sometimes, when I play, I get an error: “leaderstats is not a valid member of Player “Players.Hoogidy_Boogidy” - Client - DisplayCoinsAmount:4”. If you used Datastore 2 before, or watched Alvin Blox’s tutorial about it, then you know that you can save data in studio by adding a Boolean Value set to true in ServerStorage. If leaderstats isn’t showing up because of that, then please tell me.

I have 2 scripts. The first one is “DisplayCoinsAmount”, which is the one that gave the error and the one that displays the amount of coins the player has, in a GUI. The second script is “SaveData”, which is the one that creates the Folder, “leaderstats” and saves the coins for the player. I can’t get Roblox studio to give me the error again. It only happened twice.

Here are my 2 scripts:

DisplayCoinsAmount:

local plr = script.Parent.Parent.Parent.Parent.Parent

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

SaveData:

local DS2 = require(1936396537)
local DefaultValue = 0

game.Players.PlayerAdded:Connect(function(plr)
	local coinsDS = DS2('Coins', plr)
	local leaderstats = Instance.new('Folder', plr)
	local coins = Instance.new('IntValue', leaderstats)
	
	leaderstats.Name = 'leaderstats'
	coins.Name = 'Coins'
	
	local function updateCoins(updatedValue)
		coins.Value = coinsDS:Get(updatedValue)
	end
	
	updateCoins(DefaultValue) -- Idk what these two lines are supposed to do
	coinsDS:OnUpdate(updateCoins) -- I just copied Alvin Blox :|
	
	while wait() do
		coins.Value = coinsDS:Get()
	end
end)

I tried using game.Loaded, but I don’t really know what that does.

Code:

local DS2 = require(1936396537)
local DefaultValue = 0

game.Players.PlayerAdded:Connect(function(plr)
	local coinsDS = DS2('Coins', plr)
	
	repeat wait() until(game.Loaded)
	
	local leaderstats = Instance.new('Folder', plr)
	local coins = Instance.new('NumberValue', leaderstats)
	
	leaderstats.Name = 'leaderstats'
	coins.Name = 'Coins'
	
	local function updateCoins(updatedValue)
		coins.Value = coinsDS:Get(updatedValue)
	end
	
	updateCoins(DefaultValue)
	coinsDS:OnUpdate(updateCoins)
	
	while wait() do
		coins.Value = coinsDS:Get()
	end
end)

I would appreciate if you would help! Thank you! :grinning:

1 Like

Is the first script local or server

It’s in ServerScriptService and it’s a normal script.

Also where you said you don’t know what the 2 lines do basically it seems like the first one sets the coinsDS to the default value (0 because that’s what your variable says) and the next one tells it so that whenever the coins datastore updates to update the leaderstats coins using the function

Then why are you trying to set script.Parent.Text?

And trying to get the player using

If it wasn’t clear I was talking about this

Script

That script should be in a local script inside something like a text label also in the local script use game:GetService("Players").LocalPlayer
To get the player in a local script

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?