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’]?
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
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
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.
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