Datastore problem

hello developers hope you are well…
I have a problem with my datastore because when I leave of game it automatically saves what I had but if I buy something and I have less money left it doesn’t save

local datastore = game:GetService("DataStoreService")
local Miranura = datastore:GetDataStore("MiRanura")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	
	
	local Strength = Instance.new("IntValue",leaderstats)
	Strength.Name = "ZDFS"
	
	--datastore
	local identificacion = player.UserId
	Strength.Value = Miranura:GetAsync(identificacion)
	
	
end)
game.Players.PlayerRemoving:Connect(function(player)
	local identificacion = player.UserId
	
	Miranura:SetAsync(identificacion, player.leaderstats.ZDFS.Value)
end)

if someone can help me thank you very much
by the way if you wonder how to add money: add money from the command bar
I already checked if I had an error but apparently not. I explain better:
if I enter my game which is missing a lot: ZDF [ zona de fuego ] - Roblox
and I write in the command console that I add it to the leaderstats it adds it and saves it but if for some reason I go to the restaurant I buy something it remains but it does not save if someone can solve it please I beg you

You could save the stats when the value is changed, as well as when they leave the game.

leaderstats.ZDFS:GetPropertyChangedSignal("Value"):Connect(function()
    Miranura:SetAsync(identificacion, leaderstats.ZDFS.Value)
end)
1 Like

This is how I would do it, and as the person above me said, you could also save on every changed signal.
Also, you don’t really need to have the extra identificacion variable, player.UserId works fine.

local datastore = game:GetService("DataStoreService")
local Miranura = datastore:GetDataStore("MiRanura")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Strength = Instance.new("IntValue",leaderstats)
	Strength.Name = "ZDFS"
	
	--datastore
	local strengthdata

	local identificacion = player.UserId
	local success, errormessage = pcall(function()
		strengthdata = Miranura:GetAsync(identificacion)
	end)

	if success then
		Strength.Value = strengthdata or 0
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local identificacion = player.UserId
	local success, errormessage = pcall(function()
		Miranura:SetAsync(identificacion, player.leaderstats.ZDFS.Value)
	end)
	if success then
		print("Data stored")
	else
		print("Error saving data.")
		warn(errormessage)
	end
end)```
1 Like

Thanks a lot…
I did it like this but it didn’t work:

local datastore = game:GetService("DataStoreService")
local Miranura = datastore:GetDataStore("MiRanura")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	
	
	local Strength = Instance.new("IntValue",leaderstats)
	Strength.Name = "ZDFS"

	--datastore
	local identificacion = player.UserId
	
	local Data
	
	local ok,  errordemensaje = pcall(function()
	Data =	Miranura:GetAsync(identificacion)
	end)
	if ok == true then
		print("data cargada.")
		Strength.Value = Data
	else
		print("no se cargo la data")
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local identificacion = player.UserId
	local ok, errordemensaje = pcall(function()
		Miranura:SetAsync(identificacion, player.leaderstats.ZDFS.Value)
	end)
	if ok == true then
		print("data guargada.")
	else
		print("no se guardo la data")
	end

end)

When you’re buying something, are you updating the value on the server?
If it’s only from a localscript then your money value never actually changed.

1 Like

It is a script which is in the serverscriptservice so it works
I think I explained it wrong, sorry
If I enter the game and write /console in the chat, the command bar appears and if I add it from there to the true save leaderstats. But if I go and buy an item, the leaderstats subtracts, but if I leave, it does not save the money already subtracted.

Hmm… I’m not entirely sure what’s going on here.
The PlayerRemoving function doesn’t have anything wrong with it, it should be saving that value to player.UserId.
So if I had to guess, somehow that value isn’t being updated properly, or you’re saving the wrong value.

Bad idea. If you do this, you will be throttling data requests to the API. Doing this can result in dropping data requests from yourself and other players. This can result in data loss.
Also, make sure studio access to API services is enabled otherwise you may not be seeing your desired results.
If you do not know where it is, click setting in roblox studio and look for it.

1 Like

Yes it is enabled but what do you recommend I do?

Potentially not the best practice.

In a scaled-up game with 50 player servers: that’s a lot of requests.

Consider storing in a cache local to the server, and updating the datastore upon leaving:

local cache = plyr:SetAttribute("ds_cache", {
    ["val1"] = 1,
    ["val2"] = 2
})

game:GetService("Players").PlayerRemoving:Connect(function(plyr)
    -- update datastore
end)

Hello friend, if you could explain me a little better, I would appreciate it very much. I am new to Roblox Studio and I lack a lot of experience. Excuse me.

a question sorry if I do it as you say the data storage would not be exploited