When a value incercrases, it doesn't saves with DataStore, here is the script:
local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore") -- Creates a leaderboard that shows player variables local function onPlayerJoin(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local gold = Instance.new("IntValue") gold.Name = "Gold" gold.Value = 0 gold.Parent = leaderstats local items= Instance.new("IntValue") items.Name = "Molecules" items.Value = 0 items.Parent = leaderstats local spaces = Instance.new("IntValue") spaces.Name = "Spaces" spaces.Value = 2 spaces.Parent = leaderstats end -- Run onPlayerJoin when the PlayerAdded event fires game.Players.PlayerAdded:Connect(onPlayerJoin) local data local success, errormessage = pcall(function() myDataStore:GetAsync(player.UserId.."-items") myDataStore:GetAsync(player.UserId.."-spaces") myDataStore:GetAsync(player.UserId.."-gold") end) game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() data = myDataStore:SetAsync(player.UserId.."-",player.leaderstats.gold.Value) data = myDataStore:SetAsync(player.UserId.."-",player.leaderstats.items.Value) data = myDataStore:SetAsync(player.UserId.."-",player.leaderstats.spaces.Value) end) if success then gold.Value = data items.Value = data spaces.Value = data else print("Unexcepted Error Happened while saving your data! ") warn(errormessage) end if success then print ("playerdatasaved") else print ("playerdatanotsaved") warn(errormessage) end end)
First things first, "player" in lines 35-37 is not defined anywhere. Script doesn't understand what 'player' is.
Second, "gold", "items" and "spaces" in lines 52-54 are also not defined. Even if you create a variable in function, it doesn't work outside it.
I think your problem is, that you don't understand how to use scopes. Here's a Roblox tutorial about it: https://developer.roblox.com/en-us/articles/Scope
Also, there's a nice Roblox tutorial that explains how to correctly save player's data on exit: https://developer.roblox.com/en-us/articles/Saving-Player-Data
Making it the way you've done it is a bad practice. After player leaves the game script can't access "leaderstats" object, because it's already gone. You should do this the way as it's shown in the second link :)
And also, when you test DataStore, I recommend doing it in actual game, not in Roblox Studio. DataStore in Roblox Studio doesn't always work with game.Players.PlayerRemoving.
Hope I helped :)