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)

7 comments
Does any of "print()" actually print something?
Yes
This wont solve your issue but you can set the parent of an instance as the second argument of the instance constructor. E.g: local myPart = Instance.new("Part", workspace)
So, if yes, which one prints something?
lemme check
it doesn' t prints something (i confused with the script of the BuyButton that i putted in the game)
here you have the link of the game (wich is uncopylocked and it it's just a test for the script for the original one) LINK: https://www.roblox.com/games/5023928146/Untitled-Game