local dataStoreService = game:GetService("DataStoreService") local killSave = dataStoreService:GetDataStore("PlayerKills") game.Players.PlayerAdded:Connect(function(player) local totalKills local success, err = pcall(function() totalKills = killSave:GetASync("Player_"..player.UserId) end) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local kills = Instance.new("IntValue") kills.Name = "Kills" if success then kills.Value = totalKills else kills.Value = 0 end kills.Parent = leaderstats local rank = Instance.new("StringValue") rank.Name = "Rank" rank.Value = "Human" rank.Parent = leaderstats local function updateKills(newKills) if newKills then if newKills >= 25 then rank.Value = "Supreme" end if newKills >= 100 then rank.Value = "Sage" end end end updateKills() kills.Changed:Connect(updateKills) end) local function save(player) local success, err = pcall(function() killSave:SetAsync("Player_"..player.UserId, player.leaderstats.Kills.Value) end) if success then print("saved successfully") else print("failed to save") end end local function autosave() while wait(15) do for i, player in pairs(game:GetService("Players"):GetPlayers()) do print("saving") save(player) end end end spawn(autosave) game.Players.PlayerRemoving:Connect(function(player) save(player) end)
I have API Services allowed so I have no clue what causes the data to not store.
The PlayerRemoving event is, in some cases, not quite enough to save all data, as the server closing itself will kill this script when it tries to save the data. Sure, you are hardly saving much with that save() function, but saving to datastores can take a really long time as it takes a long time to communicate with the servers. I resolved this problem myself by using the methods in this tutorial, where it is explained that you should use :BindToClose() in addition to PlayerRemoving. Hopefully this helps!
Edit: here is exactly how I do this in my game.
game.Players.PlayerRemoving:Connect(function() SaveData(Player) end) game:BindToClose(function() SaveData(Player) end)