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)
Sorry for the late answer, but this should work!
Problem: It's pretty hard to explain but the reason it didn't work was because the playerevent was declared after the variables, when it should be on top:
(Also ensure that this is in a server script (normal script) I changed your last script!
game.Players.PlayerAdded:Connect(function(plr) local DS2 = require(1936396537) local DefaultValue = 0 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)
put wait(3) on line 1 it will wait 3 seconds so it will be loaded and wont make error bcs it will find it. this is script for saving folders. if someone need normal saving for folders :)
local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") local function onPlayerJoin(player) local folder = Instance.new("Folder", player) folder.Name = "leaderstats" local money = Instance.new("NumberValue", folder) money.Name = "Money" local playerUserId = "Player_"..player.UserId local data = playerData:GetAsync(playerUserId) if data then money.Value = data["Money"] -- here u can add more values from top else money.Value = 0 end end local function create_table(player) local player_stats = {} for _, stat in pairs(player.leaderstats:GetChildren()) do player_stats[stat.Name] = stat.Value end return player_stats end local function onPlayerExit(player) local player_stats = create_table(player) local success, err = pcall(function() local playerUserId = "Player_"..player.UserId playerData:SetAsync(playerUserId, player_stats) end) if not success then warn("Could not save data") end end game.Players.PlayerAdded:Connect(onPlayerJoin) game.Players.PlayerRemoving:Connect(onPlayerExit)