Data store doesn’t save value when I give to myself currency by game.Players.TheCookieDragonPlays.leaderstats.hiddenfolder.Coins.Value += 0
and when I rejoin the value stays at the same number as it was earlier.
Code:
local DataStoreService = game:GetService('DataStoreService')
local SaveValue = DataStoreService:GetDataStore('SaveValue')
function SaveData(player)
local success, errormessage = pcall(function()
SaveValue:SetAsync(player.UserId .. "-coins", player.leaderstats.hiddenfolder.Coins.Value)
end)
if success then
print("Player Data Saved")
else
print("Player data not saved")
warn(errormessage)
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local hiddenvalue = Instance.new("Folder")
hiddenvalue.Name = "hiddenfolder"
hiddenvalue.Parent = leaderstats
local cash = Instance.new("IntValue")
cash.Name = "Coins"
cash.Parent = hiddenvalue
local data
local success, errormessage = pcall(function()
data = SaveValue:GetAsync(player.UserId .. "-coins")
end)
if success then
cash.Value = data or 0 -- Default to 0 if data is nil
else
print("There was an error whilst getting your data")
warn(errormessage)
end
game.Players.PlayerRemoving:Connect(SaveData)
end)
game:BindToClose(function()
for _,player in pairs(game:GetService("Players"):GetPlayers()) do
SaveData(player)
end
end)