So basically I don’t understand wath is wrong in my saving data script, if you could explain me it could be great.
local DTS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local TestStore = DTS:GetDataStore("Test3")
function PlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
local Data = TestStore:GetAsync(player.UserId)
if Data then
Coins.Value = Data[1]
else
print("No data")
end
end
function PlayerRemoving(player)
local data = {}
for i, v in ipairs(player:WaitForChild("leaderstats"):GetChildren()) do
table.insert(data, v.Value)
end
print(data)
local succ, err = pcall(function()
TestStore:SetAsync(player.UserId, data)
end)
if err then
print(err)
print("failed")
end
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)
In the output there is nothing except the “No data”
local DTS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local TestStore = DTS:GetDataStore("Test3")
function PlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
local suc, res = pcall(function()
return TestStore:GetAsync(player.UserId)
end)
Coins.Value = suc and res[1] or 0
end
function PlayerRemoving(player)
local data = {}
for i, v in player.leaderstats:GetChildren() do
data[v.Name] = v.Value
end
local suc, res = pcall(function()
return TestStore:SetAsync(player.UserId, data)
end)
if not suc then
warn("Error saving data for player " .. player.Name .. ": " .. res)
end
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)
Also you don’t have to use ipairs/pairs in a for loop:
for i, v in ipairs(player.leaderstats:GetChildren()) do
table.insert(data, v.Value)
end
To
for i, v in player.leaderstats:GetChildren() do
table.insert(data, v.Value)
end
You need a BindToClose() function. The game doesn’t have enough time to save the data before it closes.
local runService = game:GetService("RunService")
game:BindToClose(function()
if runService:IsStudio() then task.wait(5) return end
for _, player in ipairs(Players:GetPlayers()) do
PlayerRemoving(player)
end
task.wait(3)
end)
extra note: always use a pcall() when accessing the DataStore, it’s a network request so it can fail.
local DTS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local TestStore = DTS:GetDataStore("Test3")
function PlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
local suc, res = pcall(function()
return TestStore:GetAsync(player.UserId)
end)
print(suc, res)
Coins.Value = (suc and res and res.Coins) or 0
end
function PlayerRemoving(player)
local data = {}
for i, v in player.leaderstats:GetChildren() do
data[v.Name] = v.Value
end
local suc, res = pcall(function()
return TestStore:SetAsync(player.UserId, data)
end)
if not suc then
warn("Error saving data for player " .. player.Name .. ": " .. res)
end
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)