It prints Data loaded and Data Saved when entering or leaving the game, but it doesn't actually save. When I join the game it still says 0.
local Datastore = game:GetService("DataStoreService"):GetDataStore('Rocky-Test91') game.Players.PlayerAdded:Connect(function(player) local Key = 'Player-ID:' .. player.userId local leaderstats = Instance.new("Folder") leaderstats.Name = 'leaderstats' leaderstats.Parent = player local Pebbles = Instance.new("IntValue") Pebbles.Name = 'Pebbles' Pebbles.Value = 0 Pebbles.Parent = leaderstats local GetSave = Datastore:GetAsync(Key) if GetSave then Pebbles.Value = GetSave print('Data Loaded for '..player.Name) else local Numbers = {Pebbles.Value} Datastore:SetAsync(Key, Numbers) end end) game.Players.PlayerRemoving:Connect(function(player) local Key = 'PlayerID:' .. player.userId local ValueToSave = {player.leaderstats.Pebbles.Value} Datastore:SetAsync(Key, ValueToSave) print('Data Saved For '..player.Name) end)
You set ValueToSave as a table, i'm not sure why but since it's a table all you're doing to calling the table itself and not the values inside it. Also it's good practice to use a BindToClose function at the end so the data will be saved when the server is about to shut down.
local Datastore = game:GetService("DataStoreService"):GetDataStore('Rocky-Test91') game.Players.PlayerAdded:Connect(function(player) local Key = 'Player-ID:' .. player.userId local leaderstats = Instance.new("Folder") leaderstats.Name = 'leaderstats' leaderstats.Parent = player local Pebbles = Instance.new("IntValue") Pebbles.Name = 'Pebbles' Pebbles.Value = 0 Pebbles.Parent = leaderstats local GetSave = Datastore:GetAsync(Key) if GetSave then Pebbles.Value = GetSave print('Data Loaded for '..player.Name) else local Numbers = {Pebbles.Value} Datastore:SetAsync(Key, Numbers) end end) game.Players.PlayerRemoving:Connect(function(player) local Key = 'PlayerID:' .. player.userId local ValueToSave = {player.leaderstats.Pebbles.Value} Datastore:SetAsync(Key, ValueToSave[1]) -- calls the value from the table print('Data Saved For '..player.Name) end) game:BindToClose(function() for i,v in pairs(game.Players:GetPlayers()) do if v then v:Kick() -- kicks all players before server shuts down so their data will save from the PlayerRemoving event end end wait(3) -- waits 3 seconds before the server shuts down so the player's data will save end)