Use a ModuleScript to store the values. (When the player is in game)
Save it using UpdateAsync. (@AbiZinho is right, it is better.)
DataStore:UpdateAsync(Player.UserId, function(OldData)
local Data = OldData or {} -- what we are going to save to.
for OldKey, OldValue in pairs(Data) do
for NewKey, NewValue in pairs(NewData) do -- two loops in order to check the difference between the tables.
if not Data[NewKey] then -- if new data was added
Data[NewKey] = NewValue
end
if not NewData[OldKey] then -- if the old key doesn't exist in the new table (aka if data was removed)
Data[OldKey] = nil
end
if Data[OldKey] and NewData[NewKey] then -- if there's a difference in values
if Data[OldKey] ~= NewData[NewKey] then
Data[NewKey] = NewValue
end
end
end
end
return Data
end)
And that’s it!