Data store Money was not saved as it was not updated

local starterValue = 0
local currentStat = "Money"
local saveDelay = 5

local function saveToDataStore(player, datastore, currentValue)
	datastore:Set(currentValue)
end

game.Players.PlayerAdded:Connect(function(player)
	local currentDataStore = datastoreModule("Money", player)

	repeat
		wait()
	until player.leaderstats and player.leaderstats:FindFirstChild(currentStat)

	local function updateStat(updatedValue)
		player.leaderstats:FindFirstChild(currentStat).Value = currentDataStore:Get(updatedValue)
	end

	updateStat(starterValue)

	currentDataStore:OnUpdate(updateStat)

	spawn(function()
		local lastValue = player.leaderstats:FindFirstChild(currentStat).Value
		while true do
			wait(300)
			local currentValue = player.leaderstats:FindFirstChild(currentStat).Value
			if currentValue ~= lastValue then
				saveToDataStore(player, currentDataStore, currentValue)
				lastValue = currentValue
			end
		end
	end)
end)

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local currentValue = player.leaderstats:FindFirstChild(currentStat).Value
		local currentDataStore = datastoreModule("Money", player)
		saveToDataStore(player, currentDataStore, currentValue)
	end
	wait(saveDelay)
end)

im not fan of ds2 but im still trying to understand . How to fix it ? i always got a warning “Data store Money was not saved as it was not updated.” and is it okay to use bind to close because some of ppl say that i dont have to use bind to close when uisng ds2

First of all, you’re not calling Combine on the data store you created. Second of all, I think it’s unnecessary to call a bind to close to it.

Anyways, the main problem here is that you’re supposed to use the Changed event on the leaderstat value and not a loop.

but my money change frequently ? like every 0.2 seconds something like that . and why i need to use Combine i kinda dont understand its like Combine . ( DATA , Money ) something like that ?

Yes.

ds 2 automatically will save data only when it needs to and it wont update it as often as data store service

wait so i dont have to use either debounce or interval because Datastore throttling will not occur ? oh damn please help me

It won’t occur, you don’t need a debounce or any of that.

1 Like

I will try that thank you for telling me

image

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

local DataStore2 = require(ServerScriptService.DATASTORE.DataStore2)

local starterValue = 0
local currentStat = script.Parent
local statFolder = "leaderstats"

Players.PlayerAdded:Connect(function(player)
	wait(0.2) -- wait before leaderstats load to make sure
	local currentDataStore = DataStore2(string.lower(script.Name), player)
	
	repeat wait()
	until player:FindFirstChild(statFolder) and player:FindFirstChild(statFolder):FindFirstChild(currentStat)
	local function updateMoney(updateDuit)
		player:FindFirstChild(statFolder):FindFirstChild(currentStat).Value = updateDuit
		
	end
	
	if currentDataStore:Get() == nil then
		updateMoney(starterValue)
		
	else
		currentDataStore:OnUpdate(updateMoney(currentDataStore:Get()))
	end
	
	player:FindFirstChild(statFolder):FindFirstChild(currentStat):GetPropertyChangedSignal("Value"):Connect(function()
		currentDataStore:Set(player:FindFirstChild(statFolder):FindFirstChild(currentStat).Value)
	end)
end)

i tried but the warning still occur ( i did not use combine , since i will use it for shop system later ) any reason ?

NVM NVM FIXED I TRIED USING SIMPLE SCRIPT AND ITS MUCH BETTER HOW IN HELL IT WORKS FINE

local DS2 = require(script.DataStore2)
local Players = game:GetService("Players")

DS2.Combine("MYFATHERYOURFATHERWEARESAME", "Money")


local function initPlayer(plr)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = plr

	local Money = Instance.new("NumberValue")
	Money.Name = "Money"
	Money.Parent = ls

	local moneyData = DS2("Money", plr)
	if moneyData:Get() ~= nil then
		Money.Value = moneyData:Get()
	else
		Money.Value = 250 
	end


	Money.Changed:Connect(function()
		moneyData:Set(Money.Value)
	end)
end


Players.PlayerAdded:Connect(initPlayer)


while wait(2) do
	for _, plr in pairs(Players:GetPlayers()) do
		plr.leaderstats.Money.Value = plr.leaderstats.Money.Value + 50
	end
end

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