Trouble with DataStores and tables

  1. What do you want to achieve?
    I want to make a table that holds all generated codes.

  2. What is the issue?
    They’re each getting put into a numbered table and I don’t know why or how to prevent it from happening.

  3. 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.

you are inserting it as a table inside the table

do this instead:

CodeTable[Code] = {["Reedemed"] = false, ["Reward"] = "Huge Amethyst Golem"}

Thanks, I’ll try this and see if it works.

Okay before I start about that, I want you to know some things:

  1. Simply calling GetAsync and SetAsync is extremely dangerous and should be avoided (see below).
  2. 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

It gave me an error.

Thanks, I will try this script

It errored, also should the DataStore script be located in a server script instead?

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.

The module script function was being called from a sever script. Also I will try fixing the error using your script. Thanks again for helping!

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.