local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
coins = Instance.new("NumberValue")
coins.Name = "Gold"
coins.Parent = leaderstats
coins.Value = 0 or myDataStore:GetAsync(player.UserId..' - Gold')
Eggs = Instance.new("NumberValue")
Eggs.Name = "Eggs"
Eggs.Parent = leaderstats
Eggs.Value = 0 or myDataStore:GetAsync(player.UserId..' - Eggs')
Milks = Instance.new("NumberValue")
Milks.Name = "Milk"
Milks.Parent = leaderstats
Milks.Value = 0 or myDataStore:GetAsync(player.UserId..' - Eggs')
end)
game.Players.PlayerRemoving:Connect(function(player)
local s,e = pcall(function()
myDataStore:SetAsync(player.UserId..' - Gold',coins.Value)
end)
if e then
warn('Failed saving "Gold" for '..player.DisplayName)
end
local s,e = pcall(function()
myDataStore:SetAsync(player.UserId..' - Eggs',Eggs.Value)
end)
if e then
warn('Failed saving "Eggs" for '..player.DisplayName)
end
local s,e = pcall(function()
myDataStore:SetAsync(player.UserId..' - Milks',Milks.Value)
end)
if e then
warn('Failed saving "Milk" for '..player.DisplayName)
end
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetChildren()) do
player:Kick("Server Closed")
end
wait(2)
end)
I think the issue is that it IS saving, you just have your loading wrong. You should have the :GetAsync part of it BEFORE the 0 — 0 is always going to exist, so the GetAsync thing is never used, hence why it appears to not save
Updated Script
local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“MyDataStore”)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”, player) – You can set the parent within the Instance.new thing
leaderstats.Name = “leaderstats”
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
coins = Instance.new("NumberValue")
coins.Name = "Gold"
coins.Parent = leaderstats
if myDataStore:GetAsync(player.UserId..' - Gold') then
coins.Value = myDataStore:GetAsync(player.UserId..' - Gold')
else
coins.Value = 0
end
Eggs = Instance.new("NumberValue")
Eggs.Name = "Eggs"
Eggs.Parent = leaderstats
if myDataStore:GetAsync(player.UserId..' - Eggs') then
Eggs.Value = myDataStore:GetAsync(player.UserId..' - Eggs')
else
Eggs.Value = 0
end
Milks = Instance.new("NumberValue")
Milks.Name = "Milk"
Milks.Parent = leaderstats
if myDataStore:GetAsync(player.UserId..' - Milks') then
Milks.Value = myDataStore:GetAsync(player.UserId..' - Milks')
else
Milks.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local s,e = pcall(function()
myDataStore:SetAsync(player.UserId..' - Gold',coins.Value)
end)
if e then
warn('Failed saving "Gold" for '..player.DisplayName)
end
local s,e = pcall(function()
myDataStore:SetAsync(player.UserId..' - Eggs',Eggs.Value)
end)
if e then
warn('Failed saving "Eggs" for '..player.DisplayName)
end
local s,e = pcall(function()
myDataStore:SetAsync(player.UserId..' - Milks',Milks.Value)
end)
if e then
warn('Failed saving "Milk" for '..player.DisplayName)
end
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetChildren()) do
player:Kick("Server Closed")
end
wait(2)
end)