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!