I’m trying to store a players leaderstat (gold) via datastores, however my script cannot seem to find the leaderstats folder that is in the player. To solve this, I’ve tried direct referencing, wait for child, and findfirstchild, but the script keeps erroring.
The error that pops up when using direct referencing is “leaderstats is not a valid player of Player Player.ModdedDreams”, and when using WaitForChild() it times out. When in game I can easily find the leaderstats folder in my player.
Datastore code:
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
local playerUserID = plr.userId
local playerGold = plr:WaitForChild("leaderstats").Gold.Value --This is where the code errors
Players.PlayerRemoving:Connect(function(plr)
local setSuccess, errorMessage = pcall(function()
goldStore:SetAsync(playerUserID, playerGold)
end)
if not setSuccess then
local errorMessage = "Error - Datastore attempt failed"
warn(errorMessage)
end
Players.PlayerAdded:Connect(function(plr)
local getSuccess, currentGold = pcall(function()
return goldStore:GetAsync(playerUserID)
end)
if getSuccess then
print('Yay!')
plr:FindFirstChild("leaderstats").Gold.Value = currentGold
local oop = plr:FindFirstChild("leaderstats").Gold.Value
print(oop)
end
end)
end)
end)
Thank you for your help, however while there are no errors, it does not seem to be setting the value of the gold leaderstat to the one stored. Any ideas?
You’re attempting to just create 1 Instance of the leaderstats variable, it’s not creating 1 every time a player joins and it being parented to the player’s object
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
Players.PlayerAdded:Connect(function(Player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = Player
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Value = 0
gold.Parent = leaderstats
local strength = Instance.new("IntValue")
strength.Name = "Strength"
strength.Parent = leaderstats
strength.Value = 0
local level = Instance.new("IntValue")
level.Name = "Rebirths"
level.Value = 0
level.Parent = leaderstats
local goldcount = script.ScreenGui.Frame.GoldCount
goldcount.Text = gold.Value
local clone = script.ScreenGui:Clone()
clone.Parent = plr.PlayerGui
while true do
gold.Value += 10
clone.Frame.Goldcount.Text = gold.Value
wait(10)
end
local Data
local success, whoops = pcall(function()
Data = goldStore:GetAsync(Player.UserId)
end)
if success and Data then
print("Yes")
gold.Value = Data
end
end)
Players.PlayerRemoving:Connect(function(Player)
local success, whoops = pcall(function()
goldStore:SetAsync(Player.UserId, Player.leaderstats.Gold.Value)
end)
if success then
print("Yes")
else
warn(whoops)
end
end)
I also went ahead and fixed a couple of other unnecessary things inside your script
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
Players.PlayerAdded:Connect(function(Player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = Player
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Value = 0
gold.Parent = leaderstats
local strength = Instance.new("IntValue")
strength.Name = "Strength"
strength.Parent = leaderstats
strength.Value = 0
local level = Instance.new("IntValue")
level.Name = "Rebirths"
level.Value = 0
level.Parent = leaderstats
local goldcount = script.ScreenGui.Frame.GoldCount
goldcount.Text = gold.Value
local clone = script.ScreenGui:Clone()
clone.Parent = plr.PlayerGui
local Data
local success, whoops = pcall(function()
Data = goldStore:GetAsync(Player.UserId)
end)
if success and Data then
print("Yes")
gold.Value = Data
end
while Players:GetPlayerByUserId(plr.UserId) do
gold.Value += 10
clone.Frame.Goldcount.Text = gold.Value
wait(10)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local success, whoops = pcall(function()
goldStore:SetAsync(Player.UserId, Player.leaderstats.Gold.Value)
end)
if success then
print("Yes")
else
warn(whoops)
end
end)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
Players.PlayerAdded:Connect(function(Player)
local welcome = script.welcome:Clone()
welcome.Parent = Player.PlayerGui
spawn(function()
wait(5)
welcome:Destroy()
end)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = Player
local gold = Instance.new("IntValue")
gold.Name = "Gold"
gold.Value = 0
gold.Parent = leaderstats
local strength = Instance.new("IntValue")
strength.Name = "Strength"
strength.Parent = leaderstats
strength.Value = 0
local level = Instance.new("IntValue")
level.Name = "Rebirths"
level.Value = 0
level.Parent = leaderstats
local goldcount = script.ScreenGui.Frame.GoldCount
goldcount.Text = gold.Value
local clone = script.ScreenGui:Clone()
clone.Parent = Player.PlayerGui
local Data
local success, whoops = pcall(function()
Data = goldStore:GetAsync(Player.UserId)
end)
if success and Data then
print("Yes")
gold.Value = Data
end
while Players:GetPlayerByUserId(Player.UserId) do
gold.Value += 10
clone.Frame.Goldcount.Text = gold.Value
wait(10)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local success, whoops = pcall(function()
goldStore:SetAsync(Player.UserId, Player.leaderstats.Gold.Value)
end)
if success then
print("Yes")
else
warn(whoops)
end
end)