Randomly Select Vehicle Hash from Pairs

I’m trying to randomly grab a vehiclehash within pairs, I’ve got it to acknowledge my list of vehicles I just need someone to guide me in the direction of selecting 1-3 cars randomly and put it into a new set of pairs instead of a static list of cars.
For example I’ve got

hashVehicles = {
	GetHashKey('seven70'),
	GetHashKey('raptor'),
    GetHashKey('superd'),
    GetHashKey('buffalo')
    
}
		if IsPedSittingInAnyVehicle(GetPlayerPed(-1)) then
			for _,k in pairs(hashVehicles) do
                if (GetEntityModel(GetVehiclePedIsIn(GetPlayerPed(-1), 0)) == k) then
                     --trigger what I want for randomly selected hashes
                end
		end

Can I just setup another set of pairs and use math.random to fill the new pairs and use that as the new for _,k in pairs(randomVehicle)?

So, you can use #table to get the length of a table. So you can do:

local randomHashes = {}

table.insert(randomHashes,hashVehicles[math.random(1,#hashVehicles)])

What this does is creates a new blank table (randomHashes) and then uses table.insert to add entries. It then chooses a random number between 1 and the number of items in the original table, and then finds the value associated with that key.

2 Likes