[Help Lua] calling random keys and values in a table

So I am working on a burglary script that pulls a random amount of a random item from a table. this is my table

cfg.items = {

    ["stolen_jewels"] = { min = 1, max = 3 },
    ["dirty_money"] = { min = 200, max = 1000 },
    ["pills"] = { min = 1, max = 7 },
    ["lockpicking_kit"] = { min = 1, max = 3 },

}

However, I can retrieve the items, but I get all the items with a random amount, this is my function and my thread.

I only want 1 item with a random amount to show be picked up.

Citizen.CreateThread(function()
	while true do
    Citizen.Wait(1)
        local coords = GetEntityCoords(PlayerPedId())
        local pair = cfg.items
        for key,value in pairs(cfg.homes) do
            if onBurglary then
                for k,search in pairs(value.spots) do
                    if Vdist(coords, search.x, search.y, search.z) < 1.4 then
                        DrawTxt3D(search.x, search.y, search.z, textSearch, 0.3)
                        if IsControlJustPressed(0, 38) then
                            BRGserver.searchForItems(pair)
                        end
                    end
                end
            end
        end
	end
end)
function vRPbrg.searchForItems(pair)
    for item,v in pairs(pair) do
        local user_id = vRP.getUserId(source)
        local found = pair[item]
        local amount = math.random(found.min,found.max)
        vRP.giveInventoryItem(user_id,item,amount,true)
    end
end

note: I know it’s vRP, but this is really a lua table question and not a framework issue. Any help would be great!

That’s because you are looping on every items if the searchForItems function.
If you want to choose a random index tou will have to store the cfg.items index in an another table.
2 solutions :

  • Write it yourself
local keys = { "stolen_jewels", "dirty_money", "pills", "lockpicking_kit" }
  • Make it auto
local keys = {}
for k,_ in pairs(cfg.items) do 
    table.insert(keys, k) 
end

Now all you have to do is picking up a random key by doing the following :

local key = keys[math.random(1, #keys)]

Then, just apply what you have done before to give the item to the player.

Also note that in your searchForItems function, in the for loop : local found = pair[item] is also equal to v.

1 Like
function vRPbrg.searchForItems(pair)
    local itemKeys = {} 
    for item,v in pairs(pair) do
        local user_id = vRP.getUserId(source)
        local amount = math.random(v.min,v.max)
        table.insert(itemKeys, item)
        local itemKeys = itemKeys[math.random(1, #itemKeys)]
        vRP.giveInventoryItem(user_id,itemKeys,amount,true)
    end
end 

I got it to pull a random key from the table, but it gives me 4 items back and I only want 1 item to be returned.

Here is what to do :

local keys = {}
for k,_ in pairs(cfg.items) do 
    table.insert(keys, k) 
end

function vRPbrg.searchForItems(pair)
    local key = keys[math.random(1, #keys)]
    local v = cfg.items[key]
    local user_id = vRP.getUserId(source)
    local amount = math.random(v.min,v.max)
    vRP.giveInventoryItem(user_id, key, amount, true)
end 

Works great, thanks a bunch!

1 Like