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

Lua Math.random with counts in table

silveralol

Advanced OT User
Joined
Mar 16, 2010
Messages
1,484
Solutions
9
Reaction score
217
hellow...
How I can use a math.random in count items?
something like it:
Code:
local count = reward[i].count[math.random(#reward[i].count)] -- I get errors using this line
is inside of a for
and then put items into a bag with the math.random count
Code:
bag:addItem(reward[i].itemid, count)
the error is:
attempt to get length of field 'count' (a number value)
 
I don't really understand what are you tring to do, but should't be just:
Code:
local count = math.random(1, reward[i].count)
 
Code:
local count = math.random(1, reward[i].count)

addItem(cid, reward[i].itemid, count)
 
Last edited:
I don't really understand what are you tring to do, but should't be just:
Code:
local count = math.random(1, reward[i].count)
Code:
local count = math.random(1, reward[i].count)

addItem(cid, reward[i].itemid, count)
same error
attempt to index a nil value
my code now
Code:
            for i = 0, #loot.common do
                local chance = math.random(3)
                if chance < 2 then
                    local count = math.random(1, reward2[i].count)
                    rewardbag:addItem(reward2[i].itemid, count)
                end
            end
edit:
Using:
Code:
local count = math.random(reward2[i].count)
works :O but sometimes not work, I don't understand why
 
You should post your entire thing, but I bet that the error is because you don't have the "0" key on the table.
Change "for i = 0, #loot.common do" to "for i = 1, #loot.common do"
 
You should post your entire thing, but I bet that the error is because you don't have the "0" key on the table.
Change "for i = 0, #loot.common do" to "for i = 1, #loot.common do"
thank you, it resolve my problem "some times works and anothers not"...
so much thank you
edit:
you can help with this ?
I want compare, if the attacker is the most damage killer ...
how use it?
Code:
if attackerPlayer == mostdamagekiller then
 ------
end
 
Last edited:
You dont really need to compare that. The script knows who the most damage player is... Why would you need an if on it? If you use mostdamagekiller it wil give the player who did the most damage. There is no need to compare an attacker.
 
But you could try something like this:

Code:
local attackers = {}

function onAttack(player, target)
local monster = target:getName():lower()

if not monster then return false end

if monster == "monster name" then
    table.insert(attackers, player:getName())
end
return true
end

function onKill(player, target, mostdamagekiller)
for i, name in ipairs(attackers) do
    if attackers(name) == mostdamagekiller then
    --script
    end
end
return true
end
 
But you could try something like this:

Code:
local attackers = {}

function onAttack(player, target)
local monster = target:getName():lower()

if not monster then return false end

if monster == "monster name" then
    table.insert(attackers, player:getName())
end
return true
end

function onKill(player, target, mostdamagekiller)
for i, name in ipairs(attackers) do
    if attackers(name) == mostdamagekiller then
    --script
    end
end
return true
end
my attempt works
Code:
if attackerPlayer == mostdamagekiller then
--
end
I don't know why don't works before :o
but thanks you, you're awesome, my script is near to be completed!!!
 
Back
Top