Table.insert() bug

I have a very strange problem with adding variables to the table

I had such a script, and it worked despite the fact that I wrote the ‘name’ instead of the ‘Name’

function save(plr)

  local items={}

  for i,v in pairs(plr.Inventory:GetChildren())do
    table.insert(items,v.price.Value,v.name) -- [150]='itemName'
  end

  if #items>0 then
    local s,err=pcall(function()
      itemsDS:SetAsync(player.Name..'-'..player.UserId,items)
    end)
    
    if s then else print(err) end
  end
end

I even tried with item[v.price.Value]=v.Name but everything is the same, and if the items are repeated with this method, they will be saved as one

script to add items:


local itemsdata

local s,err=pcall(function()
  itemsdata=itemsDS:GetAsync(player.Name..'-'..player.UserId)
end)

if s then else print(err) end

for price,name in pairs(itemsdata)do
 --[[
add item script, but they are not saved because the table size is zero
Nothing is loaded except for those items that are already written to the datastore before this bug
--]]
end

and now, since about three or four days ago, this function has stopped working. I did not change anything, everything from the inventory is written to the server. Works

print(item.price.Value,item.Name)

but not written to the table

Maybe I changed something in some service, or something else. Help me solve the problem

Just wondering, why are you inserting it with the price being the index? Don’t you want to just have the items in it? That could be something.

1 Like

This post is confusing…
You can use either Name or name, both of these work.
What you can’t do is use # operator on unordered tables.

local a = {[2] = 'a', [5] = 'b', [6] = 'c'}
print(#a) -- 0

local b = {'a', 'b', 'c'}
print(#a) -- 3

This is why your #items > 0 condition is always false.
You can structure your table this way:

local items = {
  [1] = {
    ItemName = 'abc',
    ItemPrice = 123,
    ItemLevel = 1
    -- ...
  },
 
  [2] = {
    ItemName = 'def',
    ItemPrice = 1234,
    ItemLevel = 2
    -- ...
  }

  -- ...
}

and then, you can use the # operator (or table.getn() alias).

Also, player.Name..'-'..player.UserId as a DataStore key is very bad.
Why do you need a player name in the key?
When someone changes a username, they’re gonna lose the data.

And one more thing:
if s then else print(err) end can be replaced with if not s then print(err) end

2 Likes

Writing something to the table does not work, only writing a number or name works, two at once doesn’t. I did not dare to make dictionary tables in dictionary tables because I thought that it would be difficult to read them from there.
I’ll try to do with this method

But still, I’m wondering why the writing of two values to the table stopped working, before that everything was fine

The subject is bool value and it contains data values such as price (number value), type (string value), rarity (number value), and etc. I don’t know why, but I found it so convenient :slight_smile:

1 Like

And by the way, a very important question, do you know how to transfer my old save system to the new one? More precisely, from dictionary tables to dictionary tables in dictionary tables

I’m not sure what you mean.
I never use table.insert() while setting both key and the value.
tbl[key] = val looks much more clean.

Transferring the data is pretty simple.
On player join, you need to make sure the data will load for both old structure and new structure.

1 Like

thank you very much, I tried it through dictionary in dictionary. It worked

1 Like