Help with assigning ids to objects automatically

Im making a RNG game and i need auras to save. The problem is that when I want to save the auras i need some sort of ID for each aura. How would i assign a number to each aura automatically that is the same in every server and adding more auras wont break it?

1 Like

You can use this function. It should return a true unique string everytime

Roblox | HttpService | GenerateGUID

1 Like

But it wont save that GUID. id have to copy it in manually which defeats the point. its gotta take in some info about the aura and generate a number for it.

1 Like

I’m sorry, I don’t think I’m understanding. You meant that, let’s say, every time you create a new aura, you’d want to get an instance with the ID filled already, without you manually having to do:

local aura = Aura()
local uniqueId = game:GetService("HttpService"):GenerateGUID()
aura.id = uniqueId

If that’s the case, you can make Aura object be a metatable and for the __new index you’d do this piece of code, which would return you an Aura instance with ID already filled

1 Like

Im not sure if that would work. I have a folder in Replicated storage with auras. i have a table of the aura objects for the player, but when using datastores it wont let you save objects as values in a table. So I need a way to have some sort of encoder/decoder for the aura object. The way i thought of making that was to have a function that takes in a auras name and outputs a number so i can decode that number to get the aura object in replicated storage again.

1 Like

Maybe something like this as you said in that reply:

:slightly_smiling_face:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AurasFolder = ReplicatedStorage:WaitForChild("Auras")

local AuraLookup = {}
local AuraInverseLookup = {}

local function initializeLookup()
    local auras = AurasFolder:GetChildren()
    for index, aura in pairs(auras) do
        AuraLookup[aura.Name] = index
        AuraInverseLookup[index] = aura.Name
    end
end

initializeLookup()

local function encodeAuras(auraNames)
    local encoded = {}
    for _, name in pairs(auraNames) do
        if AuraLookup[name] then
            table.insert(encoded, AuraLookup[name])
        else
            warn("Unknown Aura: " .. name)
        end
    end
    return encoded
end

local function decodeAuras(auraIdentifiers)
    local decoded = {}
    for _, id in pairs(auraIdentifiers) do
        if AuraInverseLookup[id] then
            table.insert(decoded, AuraInverseLookup[id])
        else
            warn("Unknown Aura ID: " .. tostring(id))
        end
    end
    return decoded
end

local playerAuras = {"Aura1", "Aura2", "Aura3"}
local encodedAuras = encodeAuras(playerAuras)
local loadedEncodedAuras = {1, 2, 3} -- Example data loaded from DataStore
local decodedAuras = decodeAuras(loadedEncodedAuras)

for _, auraName in ipairs(decodedAuras) do
    print(auraName)
end
2 Likes

Mmmh, then I see an option but you’d still have to use GenerateGUID, and add it to the aura object. To store in a datastore the object, you can just use JSONEncode to serialize it into string, and JSONDecode for when getting from the datastore.
2.

1 Like

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