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