Hey guys. It's my first time trying to create a game, and I've been experimenting for the past few hours, watching tutorials and all of that but I ran into a problem. I'm trying to place data saving into my game but it ends up breaking everything
Here is how the script looks before entering the Data Save code
local Players = game:GetService("Players")
local function leaderboardFunction (player)
local leaderstats = Instance.new ("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Soil = Instance.new("IntValue")
Soil.Name = "Soil"
Soil.Parent = leaderstats
Soil.Value = 0
local Cash = Instance.new ("IntValue")
Cash.Parent = leaderstats
Cash.Name = "Cash"
Cash.Value = 0
end
Players.PlayerAdded:Connect (leaderboardFunction)
local r = game.ReplicatedStorage.Click
r.OnServerEvent:Connect (function(player)
player.leaderstats.Soil.Value = player.leaderstats.Soil.Value + math.random(2,3)
end)
And here is where everything breaks, The leaderboard stops working and the labels which i have for the 2 variables Soil and Cash stop working, the code for those is below
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local Players = game:GetService("Players")
local function leaderboardFunction (player)
local leaderstats = Instance.new ("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Soil = Instance.new("IntValue")
Soil.Name = "Soil"
Soil.Parent = leaderstats
Soil.Value = 0
local Cash = Instance.new ("IntValue")
Cash.Parent = leaderstats
Cash.Name = "Cash"
Cash.Value = 0
end
local data
local success, errormessage = pcall (function()
data = myDataStore:GetAsync(player.UserId.."-cash")
data = myDataStore:GetAsync(player.UserId.."-soil")
end)
if success then
cash.Value = data
else
print("An error occured while loading your data")
warn(errormessage)
end)
local success, erorrmesage = pcall (function()
myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
myDataStore:SetAsync(player.UserId.."-soil",player.leaderstats.Soil.Value)
if success then
print ("Player data saved")
else
print ("An error occured while saving data")
warn(errormessage)
end
Players.PlayerAdded:Connect (leaderboardFunction)
local r = game.ReplicatedStorage.Click
r.OnServerEvent:Connect (function(player)
player.leaderstats.Soil.Value = player.leaderstats.Soil.Value + math.random(2,3)
end)
Here is the Console Output
16:35:06.427 leaderstats is not a valid member of Player "Players.Daki_Games" - Client - LocalScript:5
16:35:06.427 Stack Begin - Studio
16:35:06.427 Script 'Players.Daki_Games.PlayerGui.MainUI.TextCash.LocalScript', Line 5 - Studio - LocalScript:5
16:35:06.428 Stack End - Studio
When i click the error in the output it brings me to the script for "Cash"
local Lp = game.Players.LocalPlayer
while true do
wait (0.1)
script.Parent.Text = "????" .. Lp.leaderstats.Cash.Value
end
And the other one brings me to the script for "Soil"
local Lp = game.Players.LocalPlayer
while true do
wait (0.1)
script.Parent.Text = "????" .. Lp.leaderstats.Soil.Value
end
I've been stuck on this for a good time now, I've probably made a dumb mistake and I'd appreciate if anyone could help me out. Thanks

2 comments
You should use :WaitForChild() to wait for the leaderstats to be made. Replace "Lp.leaderstats" with "Lp:WaitForChild("leaderstats")
So, the GUI and Leaderboard work in game as intended, however the datasave doesnt work