How to maintain consistent number count

I’m trying to store furniture a player places as a number. So each time they place an item, the name increases by 1

-- Get total items
local TotalFurnitureItems = #HouseData.Furniture:GetChildren()
ItemClone.Name = TotalFurnitureItems + 1 -- Name it the number so we can reference it elsewhere

Simple enough. However, problems arise when you sell/store an item. What that does is gets the number you want to delete, and remove it. Let’s say you have 6 items, and you remove item number 4.

1
2
3
5
6

Now when you add a new item using the code above, it sees that there’s 5 items, adds 1, so gives it the name ‘6’ but we already have a 6. So how can I cleanly maintain this number system, by either having it look for the lowest number, or just adding onto the current number setup.

I thought of something like this

local TotalFurnitureItems = #HouseData.Furniture:GetChildren()

local Index = 0

-- Add new item
Index += 1
ItemClone.Name = TotalFurnitureItems + Index

However cause this a server side script, the index would increase when any player increases their furniture count

Cant you just rename them again by doing

for n,item in pairs(HouseData.Furniture:GetChildren()) do
item.Name = tostring(n)
end
1 Like

Cause that’s inefficient. I want it purely just use the lowest possible number that isn’t already being used