What solutions have you tried so far?
I tried searching all over the forum but I haven’t found anything.
local DS = game:GetService("DataStoreService")
local CodeDS = DS:GetDataStore("Codes")
if CodeDS:GetAsync("Global") == nil then
CodeDS:SetAsync("Global", {})
end
local CodeTable = CodeDS:GetAsync("Global")
local CodeModule = {}
function CodeModule:GenerateCode(Pet : string)
if Pet == "Huge Amethyst Golem" then
local Code = "HAG_"..math.random(10000, 99999).."_"..math.random(10000, 99999).."_"..math.random(10000, 99999)
table.insert(CodeTable, {[Code] = {["Reedemed"] = false, ["Reward"] = "Huge Amethyst Golem"}})
CodeDS:SetAsync("Global", CodeTable)
return Code
end
end
return CodeModule
If anyone can help describe the issue and how to fix it, I’d appreciate it.
Okay before I start about that, I want you to know some things:
Simply calling GetAsync and SetAsync is extremely dangerous and should be avoided (see below).
I don’t know if storing all global codes in a singular data store is reliable but I guess it’s fine.
You should use UpdateAsync instead which will get the current data and transform it to include the code.
Example:
local DS = game:GetService("DataStoreService")
local CodeDS = DS:GetDataStore("Codes")
local CodeModule = {}
local maxAttempts = 3
function CodeModule:GenerateCode(Pet : string)
if Pet == "Huge Amethyst Golem" then
local Code = "HAG_"..math.random(10000, 99999).."_"..math.random(10000, 99999).."_"..math.random(10000, 99999)
local success = false
local newTableOfCodes
for i = 1, maxAttempts do
success, newTableOfCodes = pcall(CodeDS.UpdateAsync, CodeDS, "Global", function(data)
data = data or {}
table.insert(data, {[Code] = {["Reedemed"] = false, ["Reward"] = "Huge Amethyst Golem"}})
return data
end)
if success then
break
else
warn("Couldn't update DataStore code: ", newTableOfCodes)
task.wait(i ^ 2)
end
end
return Code, newTableOfCodes -- idk if you need this but UpdateAsync returns the new value of the data store
end
end
return CodeModule
@mnillz 's method doesn’t adresss another key issue that what if a code is created in another server and then you create a code in your server, well, the code in the first server doesn’t exist anymore because you’re caching the current codes whenever it is being required
Anything related to DataStores should be in a script (server script) and not a local script. DataStores access Roblox’s data centers and trying to use them on a local script will error.
Also the error in the console looks like the current value stored within the global codes data store is a string and not a table.
DataStores access Roblox’s data centers and trying to use them on a local script will error.
Client scripts can access Roblox’s servers, as evidenced by API methods that work in local scripts and issue network calls to Roblox’s servers.
--local script
local Players = game:GetService("Players")
local Description = Players:GetHumanoidDescriptionFromUserId(1)
print(Description) --"HumanoidDescription"
Datastores can’t be accessed via client scripts because Roblox’s engineers have determined that they shouldn’t be, not because the client can’t access Roblox’s servers.