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 is 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 would appreciate if you would help! Thank you! :D
Edit:
I tried using game.Loaded, but I don't 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)

11 comments
I would reocmmend using normal datastore; much simpler.
Normal datastore is long, and DS2 saves data better.
does it show leaderstats in the player in the explorer (when you click play)?
Yes, but when I get the error, that means it isn't there.
Maybe try using "WaitForChild("")" a few times to get the player..?
maybe add a WaitForChild on leaderstats, maybe your client script is executing too fast before the server can load the leaderstats
Where would I add the WaitForChild?
Replace line 4 with -- script.Parent.Text = tostring(plr:WaitForChild("leaderstats"):WaitForChild("Coins").Value)
That only gets rid of the error. But leaderstats isn't there. So, it's just waiting for leaderstats forever. Sorry for not replying sooner!!!!!!!
What do you mean by leaderstats isn't there, is it not showing up on the top right corner of the game?
it isn't showing in my player