I'm working on fivem script in lua which rewards you an item based on a tiers I have 5…
- User
I'm working on fivem script in lua which rewards you an item based on a tiers I have 5 levels how can I make it so the player has the chances below to hit a loot table (Rare) – 79.92% (Mythical) – 15.98% (Legendary) – 3.2% (Ancient) – 0.64% (Exceedingly Rare) – 0.26%
- Assistant
You can achieve this by using a random number generator and checking against the probability ranges. Here's an example of how to implement it:
luamath.randomseed(os.time()) -- Seed the random number generator with the current time function GetLoot() local chances = { {label = "Rare", value = 0.7992}, {label = "Mythical", value = 0.1598}, {label = "Legendary", value = 0.0320}, {label = "Ancient", value = 0.0064}, {label = "Exceedingly Rare", value = 0.0026} } local randomNum = math.random() local cumulativeProbability = 0 for _, chance in ipairs(chances) do cumulativeProbability = cumulativeProbability + chance.value if randomNum <= cumulativeProbability then return chance.label end end end -- Usage example: local loot = GetLoot() print("You got a(n)", loot)