Datastore for numbers not working but my datastore for avatars works fine

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to fix my datastore for currency, because it’s been reset back to 0 with no errors or anything in output at least 10 times within the last 2 weeks.
  2. What is the issue? Include screenshots / videos if possible!
    Datastore for my currency keeps resetting but the avatar datastore is completely fine.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I haven’t found any solutions as most of them have errors and the others just aren’t similar enough.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local dss = game:GetService("DataStoreService")
local DSmoney = dss:GetDataStore("moneyBalance")

game.Players.PlayerAdded:Connect(function(player)
	
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = DSmoney:GetAsync(player.UserId)
	money.Parent = leaderstats
	if money.Value == nil then
           money.Value = 0
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	DSmoney:UpdateAsync(player.UserId, function(prev)
		if money.Value == nil then
			return money.Value
		end
		
	end)
end)

the avatar datastore is the same other than saving after you edit it, so I don’t know why the currency is having problems but the avatar editor isn’t

You’re updating it if the value is nil. So if there is a value the datastore will simply not update.

Edit: Why are you even checking if its nil in the first place? You have made it so if it is nil it will be set to 0 on PlayerAdded.

2 Likes

Is this a single player game? If not, this is poorly set up for a multiplayer game.

oh i just realized thats the one thing thats different lol, i dont know why im doing that in updateasync

if it’s poorly set up then please show me how to improve it

Ok:

  1. Don’t make the money variable global. When the Players.PlayerRemoving event fires, just get the object inside the player’s leaderstats. Making it global would mean it’ll keep being rewritten whenever someone else joins.
  2. Use pcalls. GetAsync and UpdateAsync can fail which can mess up your script.

Example pcall usage:

local success, output = pcall(function()
   return DataStore:GetAsync("key")
end)

if success then
   -- the fetch was successful
else
   -- the fetch failed
end

The “output” variable can either become the error message (if it failed) or the data returned. The data can be nil if the player or whatever you’re fetching is new.

  1. Include game:BindToClose. The server can crash or Roblox’s servers can go down. When this happens, it’s better to have a fail safe and save everyone’s data so they don’t lose it.
1 Like

alright, thanks for the help.