Why does leaderstats not show up in my player? (player.leaderstats)
Asked by
3 years ago Edited 3 years ago
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:
1 | local plr = script.Parent.Parent.Parent.Parent.Parent |
3 | game:GetService( 'RunService' ).RenderStepped:Connect( function () |
4 | script.Parent.Text = tostring (plr.leaderstats.Coins.Value) |
SaveData:
01 | local DS 2 = require( 1936396537 ) |
04 | game.Players.PlayerAdded:Connect( function (plr) |
05 | local coinsDS = DS 2 ( 'Coins' , plr) |
06 | local leaderstats = Instance.new( 'Folder' , plr) |
07 | local coins = Instance.new( 'IntValue' , leaderstats) |
09 | leaderstats.Name = 'leaderstats' |
12 | local function updateCoins(updatedValue) |
13 | coins.Value = coinsDS:Get(updatedValue) |
16 | updateCoins(DefaultValue) |
17 | coinsDS:OnUpdate(updateCoins) |
20 | coins.Value = coinsDS:Get() |
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:
01 | local DS 2 = require( 1936396537 ) |
04 | game.Players.PlayerAdded:Connect( function (plr) |
05 | local coinsDS = DS 2 ( 'Coins' , plr) |
07 | repeat wait() until (game.Loaded) |
09 | local leaderstats = Instance.new( 'Folder' , plr) |
10 | local coins = Instance.new( 'NumberValue' , leaderstats) |
12 | leaderstats.Name = 'leaderstats' |
15 | local function updateCoins(updatedValue) |
16 | coins.Value = coinsDS:Get(updatedValue) |
19 | updateCoins(DefaultValue) |
20 | coinsDS:OnUpdate(updateCoins) |
23 | coins.Value = coinsDS:Get() |