• 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 Problem with my shared exp..

ItsMeMagic

Old Tibia Player.
Joined
Sep 11, 2008
Messages
326
Reaction score
27
Location
exiva "Magic~
Problems with shared experience.

This is the problem:
Code:
Lua Script Error: [Event Interface] 
data/events/scripts/party.lua:Party@onShareExperience 
data/events/scripts/party.lua:23: bad argument #1 to 'ipairs' (table expected, got number) 
stack traceback: 
[C]: in ? 
[C]: in function 'ipairs' 
data/events/scripts/party.lua:23: in function

This is my party.lua

Code:
function Party:onJoin(player)
    return true
end

function Party:onLeave(player)
    return true
end

function Party:onDisband()
    return true
end

function Party:onShareExperience(exp)
    local sharedExperienceMultiplier = 1.20 --20%
   
    local vocationsIds = {}
   
    local vocationId = self:getLeader():getVocation():getId()
    if vocationId ~= VOCATION_NONE then
        table.insert(vocationsIds, self:getLeader():getVocation():getId())
    end
   
    for _, member in ipairs(#self:getMembers()) do
        vocationId = member:getVocation():getId()
        if not isInArray(vocationsIds, vocationId) and vocationId ~= VOCATION_NONE then
            table.insert(vocationsIds, vocationId)
        end
    end
   
    local size = #vocationsIds
    if size > 1 then
        sharedExperienceMultiplier = 1.0 + ((size * (10 + (size - 1) * 5)) / 100)
    end
   
    exp = (exp * sharedExperienceMultiplier) / (#self:getMembers() + 1)
    return exp
end

Hope you guys can help me out,
Thanks :)
 
self:getMembers is a table, when you place a # in front of it you are telling lua to return a number, its short hand for the length or size of the table, its asking lua to return the number of numerical indexes in the table.

According to the tests below, # counts the properties of the table and then checks the indexes of each to make sure it matches the count. So simple a function this # but can be hard to explain in layman's terms.

Here is an example
Code:
local x = { -- clearly there are 4 indexes of table x but # returns 1 index
   a = 5,
   b = 6,
   7,
   ['a'] = 2
}

local n = { -- this returns 0, in theory i believe it might because its index is 
-- out of bounds of its properties count.
   [10] = 5,
   [20] = 6,
   [30] = 9
}

local t = {-- since t's index starts at 1 by default, index [3] is not out of
-- bounds so # returns 3
   5,
   [3] = 9,
   6
}

print(#x, #n, #t)
-- prints
-- 1    0     3

So what you need to do is change this
Code:
    for _, member in ipairs(#self:getMembers()) do
        vocationId = member:getVocation():getId()
        if not isInArray(vocationsIds, vocationId) and vocationId ~= VOCATION_NONE then
            table.insert(vocationsIds, vocationId)
        end
    end
To this
Code:
    for _, member in ipairs(self:getMembers()) do
        vocationId = member:getVocation():getId()
        if not isInArray(vocationsIds, vocationId) and vocationId ~= VOCATION_NONE then
            table.insert(vocationsIds, vocationId)
        end
    end
 
Last edited:
Back
Top