Saving Values in the Workspace

I am currently trying to make a DataStore that saves a value that I have in the workspace, this is what I current have:

local dataStoreService = game:GetService("DataStoreService") --Service for DataStore
local DonoDataStore = dataStoreService:GetDataStore("DonoDataStore")--DataStore for Dono
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

	local Dono = game.Workspace:WaitForChild("Dono")

	local data
	local success,errormessage = pcall(function()
		data = DonoDataStore:GetAsync("Dono")
	end)
	if success then
		Dono.Value = data
		print("Data Loaded")
	else
		print("There was an error while getting data")
		warn(errormessage)
	end

remoteEvent.OnServerEvent:Connect(function()
	print("value changed")
	local success,errormessage = pcall(function()
		DonoDataStore:SetAsync("Dono",game.Workspace.Dono.Value)
	end)
	if success then
		print("Data successfully saved!")
	else
		print("There was an error when saving data!")
		warn(errormessage)
	end
end)

The remote event fires from a GetPropertyChangedSignal and fires fine, it prints “Data successfully saved” but when I go back into the game the value remains at 0. Can anyone help me with this? (It also successfully prints “Data Loaded” also.)

I assume you want to store a global value that tells you how much players have donated?

If that’s the case, you could probably simplify this a bit by writing to the data store directly, instead of using a Value Object:

local donoDS = game:GetService("DataStoreService"):GetDataStore("Donations")
local DONO_KEY = "_k"

local function onDonation(donationValue)
donoDS:UpdateAsync(DONO_KEY, function(oldValue)
local newValue = oldValue or 0
return newValue + donationValue
end)
end

If I’m understanding what you’re doing, you could call this function each time a player successfully purchases a developer product.

Again, I’m guessing here. Not entirely sure if this is what you’re trying to do, so apologies if it’s irrelevant.

I tried this out to see how I could possibly use this, but I got this error: “Callbacks cannot yield”
image
Do you know anything about this?

That error means that a callback for a certain function, yielded. (using wait, asynchronous functions, etc)

Ah I see, if this is the case then how can I make sure that the DataStore doesn’t keep queueing up?