I’m making a DataStore system (V1, not 2), but it falsely saves the player’s data when they leave.
I click a part that gives me 1 money / click, which is how I easily increase my stats.
“Set-Up…” is announced when the player joins
“Saved…” is announced when the player leaves (I used an alt account to check this)
And the leaderstats returned a 0 when I rejoined.
This is the code I’m using to save data
function playerHandler.SaveData(player)
local playerUserId = player.UserId
local success, err = pcall(function()
PLAYER_DATA:SetAsync(playerUserId, sessionData[playerUserId])
end)
if success then
print("Saved data for player "..player.Name)
else
error(tostring(err))
end
end
I even added a manual save to the click part that saves every time I click, and the log even returned that my DataStore request was queued, so it must be taking notice?
I’d be grateful if anyone could help.
function playerHandler.SetUpPlayerData(player)
local playerUserID = player.UserId
local data
local success, err = pcall(function()
data = PLAYER_DATA:GetAsync(playerUserID)
end)
if success and data then
sessionData[playerUserID] = data
if data then
player.leaderstats:WaitForChild("Money").Value = data.Money
player.leaderstats:WaitForChild("Distance").Value = data.Distance
end
elseif err then
error(tostring(err))
else
sessionData[playerUserID] = {
Money = player.leaderstats:WaitForChild("Money").Value,
Distance = player.leaderstats:WaitForChild("Distance").Value
}
print("Set-up data for "..player.Name)
end
end
PLAYER_DATA is just a GetDataStore call for a store called “playerHandler”
...
local data
local success, err = pcall(function()
data = PLAYER_DATA:GetAsync(playerUserID)
end)
if success then
print(success)
for i, v in pairs(data) do
print(i, v)
end
end
if success and data then
sessionData[playerUserID] = data
...
function playerHandler.SaveData(player)
local playerUserId = player.UserId
local success, err = pcall(function()
PLAYER_DATA:SetAsync(playerUserId, {Money = player.leaderstats.Money.Value, Distance = player.leaderstats.Distance.Value})
end)
if success then
print("Saved data for player "..player.Name)
else
error(tostring(err))
end
end
function playerHandler.SaveData(player)
local playerUserId = player.UserId
local success, err = pcall(function()
PLAYER_DATA:SetAsync(playerUserId, {
Money = player.leaderstats.Money.Value,
Distance = player.leaderstats.Distance.Value
})
end)
if success then
print("Saved data for player "..player.Name)
for i, v in pairs(sessionData[playerUserId]) do
print(i, v)
end
else
error(tostring(err))
end
end
This is inside a part that I click to give me +1 Money, and it saves every time as well (purely for debugging purposes, once I fix this I’ll remove the part anyway).