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
THAKCS…

Hey!

I got a question, when you buy something, is the script server sided?

You need to use game:BindToClose to delay the game’s closure so the game doesn’t close immediately and the datastores have time to save.

Something simple could be

game:BindToClose(function()
    wait(3)
end) 

Very inefficient but it should work, note: BindToClose can not take more than 30 seconds to complete.

Yes they are, but you can prompt purchases from the server or client.

In the future you should create your own topic.

This should works.

local playerID = player.UserId
local data = Miranura:GetAsync(playerID)

if data then
    Strength.Value = data[1]
else
    Strength.Value = [dafault_Value]
end

Change the default_Value to the default value of strength if the player first join the game.

Paste this codes below the note you wrote: “–datastore”

or just do Strength.Value = Miranura:GetAsync(identificacion) or default_value(0 or ?)

No but it’s because it’s when you buy in game (with his number value he made) to check if it’s server sided or client sided, not gamepasses lol

But you still need to save the data on game.Players.PlayerRemoving

game:BindToClose() is when server are shutting down/crashed or other reasons

One question if it is on the client side, how would I save that?

You have to make it server sided, and if it’s client sided it’s really exploitable

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.