• 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 Do not give certain access lottery reward error [0.4]

Extrodus

|| Blazera.net ||
Premium User
Joined
Dec 22, 2008
Messages
2,736
Solutions
7
Reaction score
537
Location
Canada
Code:
Lua:
local config = {
    lottery_hour = "1 Hour", -- Time to next lottery (only for broadcast message, real time you can set on globalevents.xml)
    rewards_id = {2195, 2492, 2160, 7404, 2536, 2656, 2323, 2171, 2169, 2516}, -- Rewards ID
    crystal_counts = 2, -- Used only if on rewards_id is crystal coin (ID: 2160).
    website = "yes" -- Only if you have php scripts and table `lottery` in your database!
    }
function onThink(interval, lastExecution)
    if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(cid) <= 2 then
                list[i] = tid
            end
        end

        local winner = list[math.random(1, #list)]
        local random_item = config.rewards_id[math.random(1, #config.rewards_id)]
    
        if(random_item == 2160) then
            doPlayerAddItem(winner, random_item, config.crystal_counts)
            doBroadcastMessage("[LOTTERY SYSTEM] Winner: " .. getCreatureName(winner) .. ", Reward: " .. config.crystal_counts .. " " .. getItemNameById(random_item) .. "s! Congratulations! (Next Lottery in " .. config.lottery_hour .. ")")
        else
            doBroadcastMessage("[LOTTERY SYSTEM] Winner: " .. getCreatureName(winner) .. ", Reward: " .. getItemNameById(random_item) .. "! Congratulations! (Next Lottery in " .. config.lottery_hour .. ")")
            doPlayerAddItem(winner, random_item, 1)
        end
    
        if(config.website == "yes") then
            db.executeQuery("INSERT INTO `lottery` (`name`, `item`) VALUES ('".. getCreatureName(winner) .."', '".. getItemNameById(random_item) .."');")
        end
        return true
    end
    return true
end

The problem [getPlayerAccess check]:
Lua:
    if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(cid) <= 2 then
                list[i] = tid
            end
        end

Error:
Code:
[6:40:02.401] [Error - GlobalEvent Interface]
[6:40:02.401] data/globalevents/scripts/lottery.lua:onThink
[6:40:02.402] Description:
[6:40:02.402] (internalGetPlayerInfo) Player not found when requesting player info #1

[6:40:02.402] [Error - GlobalEvent Interface]
[6:40:02.402] data/globalevents/scripts/lottery.lua:onThink
[6:40:02.402] Description:
[6:40:02.402] data/globalevents/scripts/lottery.lua:12: attempt to compare boolean with number
[6:40:02.402] stack traceback:
[6:40:02.402]   data/globalevents/scripts/lottery.lua:12: in function <data/globalevents/scripts/lottery.lua:8>

Only player online is a character with access over 1, any ideas why the error occurs? Just getting back into the coding world and thought I did everything right but apparently not, should there be an "else return true" to close the check if there is only admins online?
 
Last edited:
Solution
@Roddet - Thanks for the response! I tested the script and the results are mixed.

When 1 player is online and it is the GOD/GM, the lottery does not give a reward and no error is shown in console. (works perfectly)

However, when there is more than 1 player online; for some reason it will randomly give me the reward, sometimes it wont execute at all and the error referenced in console is:
Code:
[1:34:54.506] [Error - GlobalEvent Interface]
[1:34:54.506] data/globalevents/scripts/lottery.lua:onThink
[1:34:54.506] Description:
[1:34:54.506] (internalGetPlayerInfo) Player not found when requesting player info #1

[1:34:54.506] [Error - GlobalEvent Interface]
[1:34:54.506] data/globalevents/scripts/lottery.lua:onThink
[1:34:54.506]...
Code:
Code:
local config = {
    lottery_hour = "1 Hour", -- Time to next lottery (only for broadcast message, real time you can set on globalevents.xml)
    rewards_id = {2195, 2492, 2160, 7404, 2536, 2656, 2323, 2171, 2169, 2516}, -- Rewards ID
    crystal_counts = 2, -- Used only if on rewards_id is crystal coin (ID: 2160).
    website = "yes" -- Only if you have php scripts and table `lottery` in your database!
    }
function onThink(interval, lastExecution)
    if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(cid) <= 2 then
                list[i] = tid
            end
        end

        local winner = list[math.random(1, #list)]
        local random_item = config.rewards_id[math.random(1, #config.rewards_id)]
 
        if(random_item == 2160) then
            doPlayerAddItem(winner, random_item, config.crystal_counts)
            doBroadcastMessage("[LOTTERY SYSTEM] Winner: " .. getCreatureName(winner) .. ", Reward: " .. config.crystal_counts .. " " .. getItemNameById(random_item) .. "s! Congratulations! (Next Lottery in " .. config.lottery_hour .. ")")
        else
            doBroadcastMessage("[LOTTERY SYSTEM] Winner: " .. getCreatureName(winner) .. ", Reward: " .. getItemNameById(random_item) .. "! Congratulations! (Next Lottery in " .. config.lottery_hour .. ")")
            doPlayerAddItem(winner, random_item, 1)
        end
 
        if(config.website == "yes") then
            db.executeQuery("INSERT INTO `lottery` (`name`, `item`) VALUES ('".. getCreatureName(winner) .."', '".. getItemNameById(random_item) .."');")
        end
        return true
    end
    return true
end

The problem [getPlayerAccess check]:
Code:
    if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(cid) <= 2 then
                list[i] = tid
            end
        end

Error:
Code:
[6:40:02.401] [Error - GlobalEvent Interface]
[6:40:02.401] data/globalevents/scripts/lottery.lua:onThink
[6:40:02.402] Description:
[6:40:02.402] (internalGetPlayerInfo) Player not found when requesting player info #1

[6:40:02.402] [Error - GlobalEvent Interface]
[6:40:02.402] data/globalevents/scripts/lottery.lua:onThink
[6:40:02.402] Description:
[6:40:02.402] data/globalevents/scripts/lottery.lua:12: attempt to compare boolean with number
[6:40:02.402] stack traceback:
[6:40:02.402]   data/globalevents/scripts/lottery.lua:12: in function <data/globalevents/scripts/lottery.lua:8>

Only player online is a character with access over 1, any ideas why the error occurs? Just getting back into the coding world and thought I did everything right but apparently not, should there be an "else return true" to close the check if there is only admins online?

if getPlayerAccess(cid) <= 2 then -- cid doesn't exist
if getPlayerAccess(tid) <= 2 then -- use tid

and yes, if there is nobody in the list to reward, you should postpone the event.
 
if getPlayerAccess(cid) <= 2 then -- cid doesn't exist
if getPlayerAccess(tid) <= 2 then -- use tid

and yes, if there is nobody in the list to reward, you should postpone the event.
Lua:
function onThink(interval, lastExecution)
        if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(tid) <= 2 then
                list[i] = tid
            end
        end

Results in
Code:
[10:2:41.261] [Error - GlobalEvent Interface]
[10:2:41.261] data/globalevents/scripts/lottery.lua:onThink
[10:2:41.261] Description:
[10:2:41.261] data/globalevents/scripts/lottery.lua:16: bad argument #2 to 'random' (interval is empty)
[10:2:41.261] stack traceback:
[10:2:41.261]   [C]: in function 'random'
[10:2:41.261]   data/globalevents/scripts/lottery.lua:16: in function <data/globalevents/scripts/lottery.lua:7>
[10:2:41.261] [Error - GlobalEvents::think] Couldn't execute event: lottery

Line 16
Lua:
 local winner = list[math.random(1, #list)]
Line 7
Lua:
function onThink(interval, lastExecution)

So this would be the error because there is nobody in the list, so return true if list = 0 or?
 
Last edited:
Its because #list is returning 0, so you only need to check if list is greater than 0 to continue
Lua:
local config = {
    lottery_hour = "1 Hour", -- Time to next lottery (only for broadcast message, real time you can set on globalevents.xml)
    rewards_id = {{2195, 1}, {2492, 1}, {2160, 2}, {7404, 1}, {2536, 1}, {2656, 1}, {2323, 1}, {2171, 1}, {2169, 1}, {2516, 1}}, -- Rewards ID
    website = "yes" -- Only if you have php scripts and table `lottery` in your database!
}
function onThink(interval, lastExecution)
    if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(cid) <= 2 then
                list[i] = tid
            end
        end

        if #list > 0 then
            local winner = list[math.random(1, #list)]
            local random_item = config.rewards_id[math.random(1, #config.rewards_id)]

            doPlayerAddItem(winner, random_item[1], random_item[2])
            doBroadcastMessage("[LOTTERY SYSTEM] Winner: " .. getCreatureName(winner) .. ", Reward: " .. (random_item[2] > 1 and random_item[2].." " or "") .. getItemNameById(random_item[1]) .. "! Congratulations! (Next Lottery in " .. config.lottery_hour .. ")")

            if(config.website == "yes") then
                db.executeQuery("INSERT INTO `lottery` (`name`, `item`) VALUES ('".. getCreatureName(winner) .."', '".. getItemNameById(random_item[1]) .."');")
            end
        end
    end
    return true
end
 
@Roddet - Thanks for the response! I tested the script and the results are mixed.

When 1 player is online and it is the GOD/GM, the lottery does not give a reward and no error is shown in console. (works perfectly)

However, when there is more than 1 player online; for some reason it will randomly give me the reward, sometimes it wont execute at all and the error referenced in console is:
Code:
[1:34:54.506] [Error - GlobalEvent Interface]
[1:34:54.506] data/globalevents/scripts/lottery.lua:onThink
[1:34:54.506] Description:
[1:34:54.506] (internalGetPlayerInfo) Player not found when requesting player info #1

[1:34:54.506] [Error - GlobalEvent Interface]
[1:34:54.506] data/globalevents/scripts/lottery.lua:onThink
[1:34:54.506] Description:
[1:34:54.506] data/globalevents/scripts/lottery.lua:10: attempt to compare boolean with number
[1:34:54.506] stack traceback:
[1:34:54.506]   data/globalevents/scripts/lottery.lua:10: in function <data/globalevents/scripts/lottery.lua:6>
[1:34:54.506] [Error - GlobalEvents::think] Couldn't execute event: lottery

Looking at the code, my first thought is to change:
Lua:
if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(cid) <= 2 then
                list[i] = tid
            end
        end
to this:
Lua:
if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(tid) <= 2 then
                list[i] = tid
            end
        end

Since as @Xikini mentioned, there is no (cid) being used in this script. I have since made the change to the script, but have not "tested" it yet due to the server not being reloaded yet since people are online enjoying their hunts. However wanted to post here and get any opinions I can before I do reboot the server.
(I'll set up a Test Server now that the main server is live to public so I'll be able to run tests without worrying about effecting players.)
 
Last edited:
@Roddet - Thanks for the response! I tested the script and the results are mixed.

When 1 player is online and it is the GOD/GM, the lottery does not give a reward and no error is shown in console. (works perfectly)

However, when there is more than 1 player online; for some reason it will randomly give me the reward, sometimes it wont execute at all and the error referenced in console is:
Code:
[1:34:54.506] [Error - GlobalEvent Interface]
[1:34:54.506] data/globalevents/scripts/lottery.lua:onThink
[1:34:54.506] Description:
[1:34:54.506] (internalGetPlayerInfo) Player not found when requesting player info #1

[1:34:54.506] [Error - GlobalEvent Interface]
[1:34:54.506] data/globalevents/scripts/lottery.lua:onThink
[1:34:54.506] Description:
[1:34:54.506] data/globalevents/scripts/lottery.lua:10: attempt to compare boolean with number
[1:34:54.506] stack traceback:
[1:34:54.506]   data/globalevents/scripts/lottery.lua:10: in function <data/globalevents/scripts/lottery.lua:6>
[1:34:54.506] [Error - GlobalEvents::think] Couldn't execute event: lottery

Looking at the code, my first thought is to change:
Code:
if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(cid) <= 2 then
                list[i] = tid
            end
        end
to this:
Code:
if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(tid) <= 2 then
                list[i] = tid
            end
        end

Since as @Xikini mentioned, there is no (cid) being used in this script. I have since made the change to the script, but have not "tested" it yet due to the server not being reloaded yet since people are online enjoying their hunts. However wanted to post here and get any opinions I can before I do reboot the server.
(I'll set up a Test Server now that the main server is live to public so I'll be able to run tests without worrying about effecting players.)
Lua:
if getPlayerAccess(tid) <= 2 then -- changed
    list[#list + 1] = tid -- changed
 
if #list == 0 then -- added
    print("Not enough players for EventName to trigger.")
    return true
end
Lua:
local config = {
    lottery_hour = "1 Hour", -- Time to next lottery (only for broadcast message, real time you can set on globalevents.xml)
    rewards_id = {{2195, 1}, {2492, 1}, {2160, 2}, {7404, 1}, {2536, 1}, {2656, 1}, {2323, 1}, {2171, 1}, {2169, 1}, {2516, 1}}, -- Rewards ID
    website = "yes" -- Only if you have php scripts and table `lottery` in your database!
}

function onThink(interval, lastExecution)
    if getWorldCreatures(0) > 0 then
        local list = {}
        for i, tid in ipairs(getPlayersOnline()) do
            if getPlayerAccess(tid) <= 2 then
                list[#list + 1] = tid
            end
        end
  
        if #list == 0 then
            print("Not enough players for EventName to trigger.")
            return true
        end
      
        local winner = list[math.random(1, #list)]
        local random_item = config.rewards_id[math.random(1, #config.rewards_id)]
  
        doPlayerAddItem(winner, random_item[1], random_item[2])
        doBroadcastMessage("[LOTTERY SYSTEM] Winner: " .. getCreatureName(winner) .. ", Reward: " .. (random_item[2] > 1 and random_item[2].." " or "") .. getItemNameById(random_item[1]) .. "! Congratulations! (Next Lottery in " .. config.lottery_hour .. ")")
  
        if(config.website == "yes") then
            db.executeQuery("INSERT INTO `lottery` (`name`, `item`) VALUES ('".. getCreatureName(winner) .."', '".. getItemNameById(random_item[1]) .."');")
        end
    end
    return true
end
 
Last edited:
Solution
@Xikini - Testing now, will post back with results shortly.

Edit: After two rounds of lottery, the players have won both times. Will report any further errors, but for now it looks like its fixed!
Thank you for the help <3
 
Last edited:
Back
Top