How do I make a dictionary from code?

I have already tried looking at other threads, and their method did not work.

Is there a way I can do what I want in the title? I already tried this: dataTable[key] = value

The above is from a function of a module, im writing my own get and setter functions for profile service.

Making a dictionary is like

local dictionary = {
	"item" = 10,
}

And changing values is like

dictionary.item = 20
--or
dictionary["item"] = 20

Is that what you meant?

Its kinda of complicated. I already have a table, I just want to insert dictionary values into the already existing table. As said before, I already tried the code above but nothing happened.

Show me the table And what you want to insert.

Its a module:

local dataTable = {}

function DM:SetAsync(player: Player, key: string, value: any)
    local profile = Profiles[player]

    if profile then
        dataTable[key] = value
    end
end

I followed @okeanskiy’s tutorial on how to set it up, then I made my own getter/setter functions.

And what makes you sure it didnt work?

I tried printing data["Cash"] and it gave me error saying it didn’t exist. I set the key to “Cash” and the value to 100.

(data is dataTable)

You are changing it inside the DataTable not the profile, you might be getting the data from the profile other than the DataTable

1 Like

OH, that makes sense. I’ll try this and see if it works.

1 Like

Hmm, it still didn’t work.

I changed this: dataTable[key] = value to profile.Data[key] = value

And I got the same error saying that ‘Cash’ is nil.

Show me the module after editing and the script which requires it.

(I never used profileService but ill try to help)

1 Like
local dataTable = {}

function DM:SetAsync(player: Player, key: string, value: any)
    local profile = Profiles[player]

    if profile then
        profile.Data[key] = value
    end
end
local DataManager = require(game.ServerStorage.DataManager)

game.Players.PlayerAdded:Connect(function(player)
    local Data = DataManager:GetAsync(player)
    DataManager:SetAsync(player, "Cash", 100)

    print(player.Name .. " has " .. Data["Cash"]) -- errors here saying that its nil while its clearly not.
end)

This means the problem is in the getAsync

Nope, it just returns the profile.Data table.

Show me that function (i gtg sry, if i came back and it isn’t solved i’ll help you, you can dm me here or discord Msix29#7740 if you want, I am really sorry!)