[help] Need help with math.random

I currently have this:

function RandomNumber()
    math.randomseed( os.time() )
    return math.random(1,7)
end

function RandomMoney()
    math.randomseed( os.time() )
    return math.random(10000,30000)
end

function RandomLoot()
    Items = {
        "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common",
        "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon",
        "Rare", "Rare"
    }
    CItems = {
        "rolex", "deeds", "chains", "rings"
    } 
    UItems = { 
        "customchain", "deeds", "chains", "classifiedusb"
    } 
    RItems = {
        "icedrolex", "federalgold", "champring"
    }
    local chosen = Items[math.random(1,#Items)]

    if chosen == "Common" then
            math.randomseed( os.time() )
            return CItems[math.random(#CItems)]
    elseif chosen == "Uncommon" then
            math.randomseed( os.time() )
            return UItems[math.random(#UItems)]
    elseif chosen == "Rare" then
            math.randomseed( os.time() )
            return RItems[math.random(#RItems)]
    end
end
  
RegisterServerEvent('bankrob:loot')
  AddEventHandler('bankrob:loot', function()
    local xPlayer = ESX.GetPlayerFromId(source)
    xPlayer.addInventoryItem(RandomLoot(), RandomNumber())
    xPlayer.addMoney(RandomMoney())
end)

And I need to make it so if you get a common item it drops from 3/6 of that item, for uncommon it would be 3/4 and for rare it would be 1/2, any info on how to do this would be great Cheers! :slight_smile:

what do you mean by 3/6? you only have 4 common items
rewrite what you want to do exactly and i will try to help you.
and you also want to give them random amout of money right?

Sorry If I was not clear,
I want the randomnumber() at the top to go and make it so if it chooses a Common item they can get a math.random(3,6)
for uncommon i want it to give a
math.random(3,4)
and for rares
math.random(1,2)

EDIT: they already get a random amout of money from 10k - 30k, its just the items i need to do now, as they currently can get from 1 - 7 of the item, i just want to change that to differ from each item that is Common, Uncommon and Rare

I think i did not understand your question my fault lol if this makes it any more simple it was worth it:

I dont want them to get 3-4 of the actual items selected i want it to choose 1 item, But depending if it chooses a Rare i want it to give you 1 or 2 of the same item, not multiple items.
Sorry I’ve been looking at text all day and my brain is a little numb lol.

okay give me one minute

Thanks bud I appreciate the help :slight_smile:

i just re-organised what you have written, I don’t know if you are using a menu or whatever “chosen” goes…
I did not test it.

Items = {
  "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common",
  "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon",
  "Rare", "Rare"
}
CItems = {
  "rolex", "deeds", "chains", "rings"
} 
UItems = { 
  "customchain", "deeds", "chains", "classifiedusb"
} 
RItems = {
  "icedrolex", "federalgold", "champring"
}

function RandomLoot()
  math.randomseed(GetGameTimer())
  for i=1, #Items do
    return Items[math.random(#Items)]
  end
  if chosen == "Common" then
    for i=1, #CItems do
      return CItems[math.random(#CItems)]
    end
  elseif chosen == "Uncommon" then
    for i=1, #UItems do
      return UItems[math.random(#UItems)]
    end
  elseif chosen == "Rare" then
    for i=1, #RItems do
      return RItems[math.random(#RItems)]
    end
  end
end

function RandomNumber()
  math.randomseed(GetGameTimer())
  for i=1, #Items do
    return Items[math.random(#Items)]
  end
  if chosen == "Common" then
    return math.random(3,6)
  elseif chosen == "Uncommon" then
    return math.random(3,4)
  elseif chosen == "Rare" then
      return math.random(1,2)
  end
end

RegisterServerEvent('bankrob:loot')
AddEventHandler('bankrob:loot', function()
  local xPlayer = ESX.GetPlayerFromId(source)
  xPlayer.addInventoryItem(RandomLoot(), RandomNumber())
  xPlayer.addMoney(math.random(10000,30000))
end)
1 Like

Its not giving a item now, Im guessing its something to do with this?

  for i=1, #Items do
    return Items[math.random(#Items)]
  end

Or it dont understand what the chosen item is from common to uncommon to rare in the randomnumber function?

EDIT: also I dont know if you know this but:

Items = {
  "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common",
  "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon",
  "Rare", "Rare"
}

They are not actual items they are the rarity of the items, so its like a 10 out of 20 chance of getting common, 2 out of 20 chance of getting a rare ect,…

I edited the post

Still not getting any item, when i changed:

 for i=1, #Items do
    return Items[math.random(#Items)]
  end

to:

local chosen = Items[math.random(1,#Items)]

It was giving me items, but unsure if it was giving the correct amount since they are rare, need to add a few more on there to see if its working as intended.

It seems to be working how it should now here is what i did for it to work,

Items = {
    "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common", "Common",
    "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon", "Uncommon",
    "Rare", "Rare"
}
CItems = {
    "rolex", "deeds", "chains", "rings"
} 
UItems = { 
    "customchain", "deeds", "chains", "classifiedusb"
} 
RItems = {
    "icedrolex", "federalgold", "champring"
}
  
function RandomLoot()
    math.randomseed(GetGameTimer())
    local chosen = Items[math.random(1,#Items)]

    if chosen == "Common" then
      for i=1, #CItems do
        return CItems[math.random(#CItems)]
      end
    elseif chosen == "Uncommon" then
      for i=1, #UItems do
        return UItems[math.random(#UItems)]
      end
    elseif chosen == "Rare" then
      for i=1, #RItems do
        return RItems[math.random(#RItems)]
      end
    end
end
  
function RandomNumber()
    math.randomseed(GetGameTimer())
    local chosen = Items[math.random(1,#Items)]
    if chosen == "Common" then
      return math.random(3,6)
    elseif chosen == "Uncommon" then
      return math.random(3,4)
    elseif chosen == "Rare" then
        return math.random(1,2)
    end
end
  
RegisterServerEvent('bankrob:loot')
AddEventHandler('bankrob:loot', function()
    local xPlayer = ESX.GetPlayerFromId(source)
    xPlayer.addInventoryItem(RandomLoot(), RandomNumber())
    xPlayer.addMoney(math.random(10000,30000))
end)
1 Like

Thank you so much for your help :slight_smile:

worked?

I edited my post above the thank you, Im not sure if its working as intended it needs a lot more testing to verify that but im pretty sure it will work, although I don’t know if having 2 of the same locals in 2 functions can mess anything up, do you have any advice for that?

Really useful, I applied it to vangelico robbery to take random items from jewelery windows. Thank you !!

1 Like