DataStore not working

Hey!
I have been making a save and load datastore system and whenever trying to use SaveData or LoadData I get this:


This is a Module, but it is required and used.

local DataStoreService = game:GetService("DataStoreService")
local MainDataStore1 = DataStoreService:GetDataStore("MAINDATASTORE1CAPTAN")


local DataModule = {}

function DataModule.SaveData(TABLE, player, Value)
	if player:IsA("Player") and Value then
		local PlayerId = "Player"..player.UserId
		local Success, Message = pcall(function()
			MainDataStore1:UpdateAsync(PlayerId, Value)
		end)
		if Success then
			return true
		else
			warn("Data could not be saved for:", player)
			warn(Message)
		end
	end
end

function DataModule.LoadData(TABLE, player : Player)
	local Data
	if player:IsA("Player") then
		local PlayerId = "Player"..player.UserId
		local Success, Message = pcall(function()
			Data = MainDataStore1:GetAsync(PlayerId)
		end)
		if Success then
			return Data
		else
			warn("Data could not be loaded for:", player)
			warn(Message)
		end
	end
end

return DataModule

I don’t fully understand :UpdateAsync() yet, but I’m pretty sure that the 2nd argument must be a function
so you would do…

MainDataStore1:UpdateAsync(PlayerId, function()
	return Value
end)

or (I might be wrong on this one I only referred to the API)

local function GetValue()
	local Value = "YourValue"
    return Value
end

MainDataStore1:UpdateAsync(PlayerId, GetValue)

1 Like

It is not a function. It is the same as as SetAsync but less likely to have errors.

It for some reason wanted SetAsync. I don’t know why, but I guess you are the closest solution.

1 Like

Their solution is correct, the second argument to :UpdateAsync() must be a callback function which returns the data to be stored to the specified key.

local Success, Message = pcall(function()
	MainDataStore1:UpdateAsync(PlayerId, function(oldValue) --The callback receives the current key's value as its first argument (optional).
		return Value --Here you return whatever value should be stored for the specified key.
	end)
end)
1 Like