Hey!
I have a problem with my datastore. When i test the game it prints “Loaded data” and it prints “Saved data” throughout the game. But i still get the starter data.
local DS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local moneyStore = DS:GetDataStore("PlayerExperience")
local AUTOSAVE_INTERVAL = 60
-- Table to store each player's data during the current session
playerData = {}
local function saveData(dataStoreKey, value)
local setSuccess, errorMessage = pcall(function()
moneyStore:SetAsync(dataStoreKey, value)
end)
if not setSuccess then
print(errorMessage)
else
print("Saved data")
end
end
local function onPlayerRemoving(player)
-- Update data store key
local playerUserID = player.UserId
if playerData[playerUserID] then
saveData(playerUserID, playerData[playerUserID])
end
end
local function onPlayerAdded(player)
-- Initially get player's data from data store
local playerUserID = player.UserId
local success, storedData = pcall(function()
return moneyStore:GetAsync(playerUserID)
end)
if success and storedData then
print("Loaded data")
playerData[playerUserID] = storedData
else
print("Loading new data")
playerData[playerUserID] = {money = 0, level = 1} -- This is the data for new players.
end
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = playerData[playerUserID].money
money.Parent = leaderstats
local level = Instance.new("IntValue")
level.Name = "Level"
level.Value = playerData[playerUserID].level
level.Parent = leaderstats
wait(5)
saveData(playerUserID,playerData[player.UserId])
end
coroutine.wrap(function()
while wait(AUTOSAVE_INTERVAL) do
print("Autosave")
for i, player in pairs(Players:GetPlayers()) do
if playerData[player.UserId] then
saveData(player.UserId, playerData[player.UserId])
end
end
end
end)()
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
I had DataStore issues yesterday. I used the exact same thing I always used and I used a DataStore editor, it saved, but it wouldn’t load. I’m not sure why though.
local success, storedData = pcall(function()
return moneyStore:GetAsync(playerUserID)
end)
if success and storedData then
print("Loaded data")
playerData[playerUserID] = storedData
else
print("Loading new data")
playerData[playerUserID] = {money = 0, level = 1} -- This is the data for new players.
end
I thought pcalls() were supposed to be written like this:
local success, err = pcall(function())
In which “err” means error. I believe your code is working fine - note that your if statement will only pass if both success and storedData is satisfied. If I read this code correctly, that is impossible because storedData is actually “err”, and you can’t have a success and a error at the same time.
Try doing something like this:
local storedData = {}
local success, err = pcall(function()
storedData = moneyStore:GetAsync(playerUserID)
end)
if success and storedData ~= nil then
print("Loaded data")
playerData[playerUserID] = storedData
else
print("Loading new data")
playerData[playerUserID] = {money = 0, level = 1} -- This is the data for new players.
end
His code is right. The variable storedData would be an error message if the success is false, otherwise it will be the value returned from function so no need to change it.
It loads the data, and then saves it again 5 second later. Thats because if the player leaves fast again, the data can sometimes be lost because of some glitch. So the save data on join fixes that.
your data load will never run because pcall returns success and failure meaning
local success, storedData = pcall(function()
return moneyStore:GetAsync(playerUserID)
end)
if success and storedData then
print("Loaded data")
playerData[playerUserID] = storedData