• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Help with chance table

beenii

Well-Known Member
Joined
Jul 26, 2010
Messages
586
Solutions
1
Reaction score
58
hi, i have table for chances, works fine but have error in console


lua:46: attempt to index a nil value
stack traceback:
[C]: in function '__index'

line 46:
local count = math.random(newtable[rand].min, newtable[rand].max)

code complete:
Code:
local config = {
    [1] = {chance = 4500, min = 1, max = 3},
    [2] = {chance = 1300, min = 4, max = 5},
    [3] = {chance = 500, min = 6, max = 8},
    [4] = {chance = 300, min = 9, max = 10},
    [5] = {chance = 100, min = 11, max = 13}
}
local a = 0
local newtable = {}
for i = 1, #config do
    a = a + config[i].chance
    for j = i, config[i].chance do
        table.insert(newtable, config[i])
    end
end
local rand = math.random(1,a)
local count = math.random(newtable[rand].min, newtable[rand].max)
 
Solution
LUA:
local config = {
    [1] = {chance = 4500, min = 1, max = 3},
    [2] = {chance = 1300, min = 4, max = 5},
    [3] = {chance = 500, min = 6, max = 8},
    [4] = {chance = 300, min = 9, max = 10},
    [5] = {chance = 100, min = 11, max = 13}
}

local totalChance = 0
for i = 1, #config do
    totalChance = totalChance + config[i].chance
end

local rand = math.random(totalChance)
local index = -1
for i = 1, #config do
    if rand <= config[i].chance then
        index = i
        break
    end
end

if index > -1 then
    local count = math.random(config[index].min, config[index].max)
    -- other stuff
end
LUA:
local config = {
    [1] = {chance = 4500, min = 1, max = 3},
    [2] = {chance = 1300, min = 4, max = 5},
    [3] = {chance = 500, min = 6, max = 8},
    [4] = {chance = 300, min = 9, max = 10},
    [5] = {chance = 100, min = 11, max = 13}
}

local totalChance = 0
for i = 1, #config do
    totalChance = totalChance + config[i].chance
end

local rand = math.random(totalChance)
local index = -1
for i = 1, #config do
    if rand <= config[i].chance then
        index = i
        break
    end
end

if index > -1 then
    local count = math.random(config[index].min, config[index].max)
    -- other stuff
end
 
Solution
repaste it
i tried to edit it before you caught it

I think it's working, I see that you created: function Monster:eek:nSpawn(position)


Could you take a look at my code and help me optimize it?

zx9oog.png
 
Last edited by a moderator:
Back
Top