• 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!

Interval on array help

boby psaico

Member
Joined
Oct 17, 2009
Messages
84
Reaction score
5
Hello guys :D
Well, how can i do a interval on array?

Example:
Current script:
Code:
local results = {
[1] = {
      [1] = 2000,
      [2] = 2000,
      [3] = 2000,
      [4] = 2000,
      [5] = 2001,
    },
}

SetPlayerStorageValue(cid, results[1][math.random(5)], 1)


I want something like this:
Code:
local results = {

[1] = {
      [{1, 4}] = 2000, --- INTERVAL
      [5] = 2001,
    },
}

SetPlayerStorageValue(cid, results[1][math.random(5)], 1)

It's possible? Thanks :)
 
Last edited:
Not tested but should work.

Code:
local results = {
    [1] = {
        [{1, 4}] = 2000, --- INTERVAL
        [5] = 2001,
    }
}

function getResultFromTable(table, value)
    for ind, v in pairs(table) do
        if type(ind) == "table" then
            if value >= ind[1] and value <= ind[2] then
                return v
            end
        else
            if value == ind then
                return v
            end
        end
    end

    return nil
end

-- use it like this:
-- local result = getResultFromTable(results[1], 4)
-- local result = getResultFromTable(results[1], 5)
 
I dont understand how to implement it to my script, it has several arrays

Code:
function onDeath(cid, corpse, deathList)
local results = {
    [1] = {
      [1] = 2000,
      [2] = 2000,
      [3] = 2000,
      [4] = 2000,
      [5] = 2001
},
    [2] = {
      [1] = 2000,
      [2] = 2000,
      [3] = 2000,
      [4] = 2000,
      [5] = 2002
},
      
    [3] = {
      [1] = 2000,
      [2] = 2000,
      [3] = 2000,
      [4] = 2000,
      [5] = 2003
}
}

SetPlayerStorageValue(cid, results[1][math.random(5)], 1)
SetPlayerStorageValue(cid, results[2][math.random(5)], 1)
SetPlayerStorageValue(cid, results[3][math.random(5)], 1)
    return true
end
 
Last edited:
getResultFromTable(results[1], math.random(5))
getResultFromTable(results[2], math.random(5))
getResultFromTable(results[3], math.random(5))
 
one more question, I need to put the maximum number of each array or I can express with var?
Example:

function onDeath(cid, corpse, deathList)
local results = {
[1] = {
[{1, 4}] = 2000,
[5] = 2001
},

[2] = {
[{1, 4}] = 2000,
[5] = 2002
},

[3] = {
[{1, 4}] = 2000,
[5] = 2003
}
}

SetPlayerStorageValue(cid, getResultFromTable(results[1], math.random(5)), 1) <-- red number
SetPlayerStorageValue(cid, getResultFromTable(results[2], math.random(5)), 1) <-- red number
SetPlayerStorageValue(cid, getResultFromTable(results[3], math.random(5)), 1) <-- red number
return true
end

I'd like something to find a max number automatically, like #results[1], but with getResultFromTable isnt possible :s
 
I don't think there is a efficient way to do that. You can add a field called "max" to the table, which holds the maximum index and then use something like:
SetPlayerStorageValue(cid, getResultFromTable(results[3], math.random(results[3].max)), 1)
 
Back
Top