How to remove a certain amount of values in a table

i have a inventory table that has all the player items, and i need to remove a certain amount of “materials” inorder to craft stuff

TableName.Materials -= 10
this will remove 10 from ‘Materials’ in the table called ‘TableName’

to tell you, they are not numbervalues, they are just stringvalues.
Example Inventory:

 [1] = "premium great sword",
                          [2] = "true devil skull sword",
                          [3] = "devil king armor",
                          [4] = "devil skull sword",
                          [5] = "true devil skull sword",
                          [6] = "divine sanctifier",
                          [7] = "light rain",
                          [8] = "blackhole",
                          [9] = "umbral summoner",
                          [10] = "iron sword",
                          [11] = "king's aura",
                          [12] = "wood sword",
                          [13] = "god's waraxe",
                          [14] = "god's waraxe",
                          [15] = "hell ingot",
                          [16] = "iron sword",
                          [17] = "iron sword",
                          [18] = "iron sword",
                          [19] = "iron sword",
                          [20] = "iron sword",
                          [21] = "iron sword",
                          [22] = "iron sword",
                          [23] = "iron sword",
                          [24] = "iron sword",
                          [25] = "iron sword",
                          [26] = "iron sword",
                          [27] = "iron sword",
                          [28] = "iron sword",
                          [29] = "iron sword",
                          [30] = "iron",
                          [31] = "iron",
                          [32] = "iron",
                          [33] = "iron",
                          [34] = "iron",
                          [35] = "iron",
                          [36] = "iron",
                          [37] = "iron",
                          [38] = "iron",
                          [39] = "iron",
                          [40] = "iron",
                          [41] = "iron",
                          [42] = "iron",
                          [43] = "iron",
                          [44] = "test sword"
local RemoveMaterials = function(Table, Material, Amount)
	local Removed = 0
	repeat 
		for i,v in pairs(Table) do 
			if (v == Material) then 
				table.remove(Table, i)
				Removed += 1
			end
		end
	until (Removed >= Amount)
end
RemoveMaterials(TableHere, "iron sword", 10)

Do what @CZXPEK posted, but I think it would be more ideal to change how your inventory saves information.

playerInventory = {
   ["iron sword"] = {count = 10},
   ["wood sword"] = {count = 0},
}
2 Likes

if i do that, all my players data would completely disappear

You could just write a script that checks for your old table formatting and converts it to a newer, more efficient formatting.

i could, but it would take forever and alot of work due to alot of scripts using my format

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.