(Accidentally posted the same topic without finishing the post, this is not a duplicate.)
It used to work no problem, until all of a sudden it stopped saving data. My XP is stuck at last saved/old value; whenever it increases, and then rejoin, it turns back to old value. I realized it when someone from my server said that their XP nor other data is not being saved anymore.
I don’t know what caused this. I didn’t change the script recently other than adding like 2 more stats, which is no different in the duplicated code except the name change (I’m not exactly sure about the time it stopped working).
Weird thing is, I put print() after playerData:SetAsync() and it prints “Saved!” when I leave the game, but in reality it does not.
Access to API Services is always enabled. The script is server-sided located in ServerScriptService.
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local hiddenstats = Instance.new("Folder")
hiddenstats.Name = "hiddenstats"
hiddenstats.Parent = leaderstats
local sample = Instance.new("IntValue")
sample.Name = "Sample"
sample.Parent = hiddenstats
local dsc = Instance.new("IntValue")
dsc.Name = "Distance"
dsc.Parent = hiddenstats
local min = Instance.new("IntValue")
min.Name = "Minute"
min.Parent = hiddenstats
local pizza = Instance.new("IntValue")
pizza.Name = "Pizza"
pizza.Parent = hiddenstats
local coffee = Instance.new("IntValue")
coffee.Name = "Coffee"
coffee.Parent = hiddenstats
local exp = Instance.new("IntValue")
exp.Name = "Experience"
exp.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
sample.Value = data['Sample']
dsc.Value = data['Distance']
exp.Value = data['Experience']
min.Value = data['Minute']
pizza.Value = data['Pizza']
coffee.Value = data['Coffee']
else
sample.Value = 0
dsc.Value = 0
exp.Value = 0
min.Value = 0
pizza.Value = 0
coffee.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetDescendants()) do
if stat.ClassName == "IntValue" then
player_stats[stat.Name] = stat.Value
end
end
return player_stats
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
print("Saved!") --It does print this in output, however no data is saved when rejoined
end)
if not success then
warn('Failed while saving data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
