@nicemike40 So I happened to fix the issue by using this modulescript:
local dataModule = {}
local err = warn
function dataModule:save(key, data, dataStore, defaultOldData, typeOf)
if data then
local success, errorDebug = pcall(function()
dataStore:UpdateAsync(key, function(oldData)
local previousDataId-- = oldData[1] or oldData.DataId or defaultOldData
if oldData then
previousDataId = oldData[1]
else
if oldData then
if not oldData[1] then
if oldData.DataId then
previousDataId = oldData.DataId
end
end
end
end
if not previousDataId then
previousDataId = defaultOldData[1] or defaultOldData.DataId
end
local currentDataId = data[1]
local where = "A"
if not currentDataId then
where = "B"
currentDataId = data.DataId
end
if not currentDataId then
warn("Could not save data. Please go to game.ServerScriptService.DataModule, and read the comments at the top of the script!")
return nil
end
if previousDataId == currentDataId then
if where == "A" then
data[1] = data[1] + 1
end
if where == "B" then
data.DataId = data.DataId + 1
end
return data
else
return nil
end
end)
end)
if success then
print("Successfully saved data of type: "..typeOf)
else
err(errorDebug)
end
return success, errorDebug
end
end
function dataModule:load(key, dataStore)
local data = nil
local success, errorDebug = pcall(function()
data = dataStore:GetAsync(key)
end)
if success then
else
err(errorDebug)
end
return {["success"] = success, ["errorDebug"] = errorDebug, ["data"] = data}
end
return dataModule
Now, to answer your questions on why I return nil, it’s because if the script detects that the data could not load, it will return nil thus not saving the data.
So basically, if I had a million cash in the game, and the script couldn’t load my cash, the loaded cash would be 0, so if I would leave the game it would override the million cash with 0, thus causing dataloss. Thats why we return nil if the data could not load. Because update async doesn’t save the data if we return nil.
-- don't use next lol
Also, why should I not use next
lol?
-- @nicemike40 using a weak-keyed map from player objects to data so that
-- the entry is cleaned up if you leave the game
setmetatable({}, {__mode = "k"})
Where should I go, to learn more about metatables?