How to add item into dictionary

data[plr]={
		['XP'] = 0;
		['Prestige'] = 0;
		['Rubys'] = 0;
		['LastCrosshair'] = 0;
		['Knives'] = {};
		['Effects'] = {};
		['CodesRedeemed'] = {};
		['LastClan'] = {};
}
local function addKnife(plr,item,quant)

local route=data[plr]['Knives']
-- how do i add the item into the route
print(route)

end

Not sure I’m supposed to use table.insert because that didnt work (said nil value to arguement #1) so how else would I go about adding something into the data[plr][‘Knives’]?

2 Likes
local function addKnife(plr,item,quant)

local route=data[plr]['Knives']
table.insert(data[plr]["Knives"], item)
print(route)

end

game.Players.PlayerAdded:Connect(function(plr)

dataTableCreate(plr)
addKnife(plr,'test',2)

end)

This is what I tried, the error I got was;

 ServerScriptService.Serverside.Main:72: invalid argument #1 to 'insert' (table expected, got nil)  -  Server - Main:72

Why dont you just index the new knife into the Knives table along with the amount?

['Knives'] = {};
["Knives"]["DefaultKnife"] = 2

I took your advice and switched it around a bit and used this instead:

local function addKnife(plr,item,quant)

local route=data[plr]
route['Knives']=item
wait(0.2)
print(data[plr])

end

It works now, thanks for your help!

1 Like

actually i realized it wasnt adding the knife to the table but rather making it the only knife in the table. how do i make it add item instead of making route[‘Knives’][item] the only item

Im not sure im understanding it wont be the only item, unless you keep using the same key name and it’ll override the current one

local Knives = {}
Knives["Knive1"] = 1
Knives["Knive2"] = 4
Knives["Knive4"] = 3

print(Knives)

Expected Output
image

This isn’t optimal at all for organization, ill show you how I have it.

data[plr]={
		['XP'] = 0;
		['Prestige'] = 0;
		['Rubys'] = 0;
		['LastCrosshair'] = 0;
		['Knives'] = {};
		['Effects'] = {};
		['CodesRedeemed'] = {};
		['LastClan'] = {};
}

Using datastores each section is automatically filled with the players data. However I want to know how to add a string to the Knives section?

You shouldn’t be getting this error AFAICT. Is your data table actually populated at the time when you call addKnife? It’s hard to tell when you don’t post enough of your script. Try this:

local function addKnife(player, knife)
    assert(player)
    local playerData = data[player]
    assert(playerData)
    local playerKnives = playerData["Knives"]
    assert(playerKnives)
    table.insert(playerKnives, knife)
end

It should error at the step that goes wrong, helping to figure out how to fix it.

No assertions failed, still getting this error.

ServerScriptService.Serverside.Main:75: invalid argument #1 to 'insert' (table expected, got string)  -  Server - Main:75

u should assign the knfies first

local function addKnife(plr,item,quant)

table.insert(data[plr], "Knives")
local route = data[plr]['Knives']
route[item] = item

end

why would i insert knives into the players data table if it already exists when the player joins?

mb didnt notice then just do the rest of the code

It seems like playerData["Knives"] is not what you are expecting to be. Notice in the error message it says “got string” when we are wanting it to be a table.

You can to the classic print debugging to see what is going on hopefully. I.e.

local function addKnife(player, knife)
    assert(player)
    local playerData = data[player]
    print(playerData)
    assert(playerData)
    local playerKnives = playerData["Knives"]
    assert(playerKnives)
    print(`playerKnives = {playerKnives}`)
    table.insert(playerKnives, knife)
end

Its still not working, saying it got a string instead of a table.

The code I shared wasn’t meant to fix it but rather to get more information about what is going wrong.

You don’t always need a dictionary, but just the value, then a comment showing what it means.

A regular dictionary:

local dictionary = {coins = 100,level = 3}

A table:

local Table = {100,3} --Coins, Level

You can just have a table of values and other tables, without needing the name, but you have comments to remind you. I use this and it works.