Need help with a random value being added to a k, v argument

My code:


ITEM.functions.Open = {
	
	icon = "icon16/briefcase.png",
	sound = "physics/body/body_medium_impact_soft1.wav",
	onRun = function(item)
			local client = item.player
			local position = item.player:getItemDropPos()
			local inventory = client:getChar():getInv()
			local ranNum = math.random(1,5);
			for k, v in pairs(item.items(ranNum)) do
				if (IsValid(client) and client:getChar() and !client:getChar():getInv():add(v)) then
					nut.item.spawn(v, position)
				end

			end
	end
}


ITEM.items1 = {"water", "cigarettes", "beans"}
ITEM.items2 = {"water", "razors", "beans"}
ITEM.items3 = {"water", "bleach", "beans"}
ITEM.items4 = {"beer", "cigarettes", "beer"}
ITEM.items5 = {"beer", "water", "beans"}


The important part is this:


local ranNum = math.random(1,5);
			for k, v in pairs(item.items(ranNum)) do
				if (IsValid(client) and client:getChar() and !client:getChar():getInv():add(v)) then
					nut.item.spawn(v, position)
				end

When I run it I get this error:



[ERROR] gamemodes/hl2rp/schema/items/sh_rationtest.lua:21: attempt to call field 'items' (a nil value)
  1. onRun - gamemodes/hl2rp/schema/items/sh_rationtest.lua:21
   2. unknown - gamemodes/nutscript/gamemode/core/libs/sh_item.lua:659
    3. func - gamemodes/nutscript/gamemode/core/libs/thirdparty/sh_netstream2.lua:117
     4. unknown - lua/includes/extensions/net.lua:32



I basically want the “items” in for “k, v in pairs(item.items(ranNum)) do” to have the value of the ranNum that gets picked. What do I have to append to the (item.items(ranNum)) to get it to do that?

For starters, that is not how you make an array. See below;



ITEM.items = {}
ITEM.items[1] = {"water", "cigarettes", "beans"}
ITEM.items[2] = {"water", "razors", "beans"}
ITEM.items[3] = {"water", "bleach", "beans"}
ITEM.items[4] = {"beer", "cigarettes", "beer"}
ITEM.items[5] = {"beer", "water", "beans"}


That is not how you read from an array. See below.



item.items[ranNum]


Disclaimer: I’m not sure what addon this is for, or whether this will work ‘as-is’

You should be using not ()

[LUA] item.items[ranNum] [/LUA]

Both of those work, thank you!