Datastore Error (Failed to load codes from DataStore: Argument 1 missing or nil)

Hello everyone!
I’m making a UGC Code Redeem System and I need a data store to put the code data.

Please help me!

Error Code :
Failed to load codes from DataStore: Argument 1 missing or nil

Code (in a Module Script) :

local CodeManager = {}

local codes = {}
local DataStoreService = game:GetService("DataStoreService")
local codeDataStore = DataStoreService:GetDataStore("CodeDataStore")

function CodeManager.AddCode(code, itemID, quantity, creatorUserID)
	if code == "" then
		warn("Key name can't be empty.")
		return
	end

	codes[code] = {
		ItemID = itemID,
		Quantity = quantity,
		CreatorUserID = creatorUserID,
		RedeemerUserID = nil,
		RedeemedAt = nil,
	}

	local success, err = pcall(function()
		codeDataStore:SetAsync(code, codes[code])
	end)

	if not success then
		warn("Failed to save code to DataStore:", err)
	end
end

function CodeManager.GetCodeInfo(code)
	return codes[code]
end

function CodeManager.RedeemCode(code, redeemerUserID)
	local codeInfo = codes[code]

	if codeInfo and not codeInfo.RedeemerUserID then
		codeInfo.RedeemerUserID = redeemerUserID
		codeInfo.RedeemedAt = os.time()

		local success, err = pcall(function()
			codeDataStore:SetAsync(code, codes[code])
		end)

		if not success then
			warn("Failed to save updated code to DataStore:", err)
		end

		return true
	else
		return false
	end
end

function CodeManager.RemoveCode(code)
	codes[code] = nil

	local success, err = pcall(function()
		codeDataStore:RemoveAsync(code)
	end)

	if not success then
		warn("Failed to remove code from DataStore:", err)
	end
end

function CodeManager.LoadCodesFromDataStore()
	
	local success, result = pcall(function()
		local keys = codeDataStore:GetAsync():GetKeys()

		for _, key in pairs(keys) do
			local success, value = pcall(function()
				return codeDataStore:GetAsync(key)
			end)

			if success then
				codes[key] = value
			else
				warn("Failed to load code from DataStore:", value)
			end
		end
	end)

	if not success then
		warn("Failed to load codes from DataStore:", result)
	end
end

return CodeManager

1 Like

You are using

local keys = codeDataStore:GetAsync():GetKeys()

But GetAsync(Key) takes a key argument.
Instead use

local keys = codeDataStore:ListKeysAsync()

Also, I suggest you use ipairs instead of pairs.

3 Likes

Thank You for helping, I have a new error now

Failed to load codes from DataStore: ServerScriptService.CodeManager:72: invalid argument #1 to ‘ipairs’ (table expected, got Instance)

Code :
for _, key in ipairs(keys) do
local success, value = pcall(function()
return codeDataStore:GetAsync(key)
end)

I have never had experience with this way of getting DS keys, but DataStoreKeyPages are instances which derived from Pages. You’ll have to get the table through :GetCurrentPage() which returns the arrays.

2 Likes

Can you make a code exemple please?

1 Like

You should read documentations before coming up here,

local keys = codeDataStore:ListKeysAsync():GetCurrentPage()
2 Likes

Please see this new post :

1 Like