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

RevScripts Support revscript

LeOnArd0

Member
Joined
Jan 24, 2010
Messages
76
Reaction score
14
Hello guys, good night!

I've been trying to adapt some scripts to revscripts and I've had good results. But sometimes I get stuck haha.


I tried to adapt this script, however, I have 3 problems.
1. He doesn't send the message when he picks up a legendary item.
2. It is not removing the item from the first and last floors (sometimes it does, sometimes it doesn't).
3. It is returning an error in the console:

Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
/data/scripts/lottery.lua:83: attempt to index a nil value
stack traceback:
[C]: in function '__index'
data/scripts/lottery.lua:83: in function </data/scripts/lottery.lua:67>

This is code:
Lua:
local decayItems = {
    [1945] = 1946, [1946] = 1945
}
local slots = {
    Position(32344, 32218, 4), Position(32345, 32218, 4), Position(32346, 32218, 4), Position(32347, 32218, 4),    Position(32348, 32218, 4),
    Position(32349, 32218, 4), Position(32350, 32218, 4)
}

local itemtable = {
    [1] = {id = 2390, chance = 100},
    [2] = {id = 2523, chance = 5},
    [3] = {id = 2646, chance = 10},
    [4] = {id = 2505, chance = 15},
    [5] = {id = 2469, chance = 20},
    [6] = {id = 2506, chance = 25},
    [7] = {id = 2471, chance = 30},
    [8] = {id = 2472, chance = 35},
    [9] = {id = 5184, chance = 40},
    [10] = {id = 5175, chance = 45},
    [11] = {id = 5185, chance = 50},
    [12] = {id = 2466, chance = 55},
    [13] = {id = 2195, chance = 60},
    [14] = {id = 5270, chance = 80},
    [15] = {id = 5181, chance = 85},
    [16] = {id = 2488, chance = 90},
    [17] = {id = 2477, chance = 95},
    [18] = {id = 5226, chance = 99}
}

local roulette = Action()

local function ender(cid, position)
    local player = Player(cid)
    local posicaofim = Position(32347, 32218, 4) -- HERE WILL APPEAR THE EFFECT, which defines the item that the player won
    local item = Tile(posicaofim):getTopDownItem()
    if item then
        local itemId = item:getId()
        posicaofim:sendMagicEffect(19)
        player:addItem(itemId, 1)
    end
    local alavanca = Tile(position):getTopDownItem()
    if alavanca then
        alavanca:setActionId(18562) -- back actionid to play again.
    end
    if itemId == 2390 or itemId == 5185 or itemId == 2471 or itemId == 2466 or itemId == 2646 then -- check if it is the ID of the LEGENDARY item
        Game.broadcastMessage('This player ' ..player:getName().. ' reward a ' ..item:getName().. ' !', MESSAGE_STATUS_WARNING) -- if it's a rare item send it on the broadcast
       
        for _, pid in ipairs(getPlayersOnline()) do
            if pid ~= cid then
                pid:say("This player "..player:getName().." reward a "..item:getName().."", TALKTYPE_MONSTER_SAY) -- if not legendary, send a regular message
            end
        end
    end
end

local function delay(position, aux)
    local item = Tile(position):getTopDownItem()
    if item then
        local slot = aux + 1
        item:moveTo(slots[slot])
    end  
end

local function exec(cid)
    -- calculate a chance and assign an item
    local rand = math.random(1, 100)
    local aux, memo = 0, 0
    if rand >= 1 then
        for i = 1, #itemtable do
            local randitemid = itemtable[i].id
            local randitemchance = itemtable[i].chance
            if rand >= randitemchance then
                aux = aux + 1
                memo = randitemchance
            end
           
        end
    end
    -- Step one: Create an item in the first SLOT, and delete if there is an item in the last slot.
    Game.createItem(itemtable[aux].id, 1, slots[1])
    slots[1]:sendMagicEffect(CONST_ME_POFF)
    local item = Tile(slots[#slots]):getTopDownItem()
    if item then
        item:remove()
    end
    -- Step two: Move items to the next slot in all slots 1 to 12 for 2 > 13
    local maxslot = #slots-1
    local lastloop = 0
    for i = maxslot, 1, -1 do
       
        addEvent(delay, 1*1*900, slots[i], i)
    end
end

function roulette.onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    if not player then
        return false
    end
    if not player:removeItem(5197, 1) then -- roulette item
        return false
    end
   
    item:transform(decayItems[item.itemid])
    item:decay()  
    --change actionid for use only one person
    item:setActionId(18563)
   
    local segundos = 15
    local loopsize = segundos*2
   
    for i = 1, loopsize do
        addEvent(exec, 1*i*500, cid.uid)
    end
    addEvent(ender, (1*loopsize*500)+1000, cid.uid, fromPosition)
   
    return true
end

roulette:aid(18562)
roulette:register()

could someone help me?

Tkss,
Leo
 
Last edited:
The line numbering is slightly wrong in the error message.

It says the function being called is 67.. but the function in your post starts at 64..

Thus the error isn't on line 83.. the error is on line 80.

Game.createItem(itemtable[aux].id, 1, slots[1])

slots is a bunch of positions.. but itemtable starts it's index at 1.

attempt to index a nil value tells us it's trying to find something that doesn't exist..

aux is defaulted to 0.. and there's no guarantee that it will be updated.. unless there's a chance of 100 in the itemtable
Which there appears to be...

So I'm not sure what the error could be from.

Aux is the likely culprit, but I don't see how it could fail.

nevertheless.. I'd put a print on aux.. and check what it's showing when the error pops up.

so above here
Lua:
Game.createItem(itemtable[aux].id, 1, slots[1])
add
Lua:
print("aux = " .. aux)
Game.createItem(itemtable[aux].id, 1, slots[1])
 
The line numbering is slightly wrong in the error message.

It says the function being called is 67.. but the function in your post starts at 64..

Thus the error isn't on line 83.. the error is on line 80.

Game.createItem(itemtable[aux].id, 1, slots[1])

slots is a bunch of positions.. but itemtable starts it's index at 1.

attempt to index a nil value tells us it's trying to find something that doesn't exist..

aux is defaulted to 0.. and there's no guarantee that it will be updated.. unless there's a chance of 100 in the itemtable
Which there appears to be...

So I'm not sure what the error could be from.

Aux is the likely culprit, but I don't see how it could fail.

nevertheless.. I'd put a print on aux.. and check what it's showing when the error pops up.

so above here
Lua:
Game.createItem(itemtable[aux].id, 1, slots[1])
add
Lua:
print("aux = " .. aux)
Game.createItem(itemtable[aux].id, 1, slots[1])
Thanks for the answer.

Good, i include this line and returned this error in console:
2023-04-06_17-35-13.026733 Lua Script Error: [Main Interface]
2023-04-06_17-35-13.026941 in a timer event called from:
2023-04-06_17-35-13.027342 (Unknown scriptfile)
2023-04-06_17-35-13.027462 /home/Servidor/data/scripts/lottery.lua:85: attempt to index a nil value
2023-04-06_17-35-13.027535 stack traceback:
2023-04-06_17-35-13.027647 [C]: in function '__index'
2023-04-06_17-35-13.027792 /home/Servidor/data/scripts/lottery.lua:85: in function </home/Servidor/data/scripts/lottery.lua:68>

2023-04-06_17-35-13.524967 aux = 5
2023-04-06_17-35-14.025068 aux = 13
2023-04-06_17-35-14.525394 aux = 3
2023-04-06_17-35-15.025484 aux = 4
2023-04-06_17-35-15.525304 aux = 17
2023-04-06_17-35-16.025596 aux = 14
2023-04-06_17-35-16.525774 aux = 12
2023-04-06_17-35-17.025632 aux = 5
2023-04-06_17-35-17.525667 aux = 4

This is updated script:
Lua:
local decayItems = {
    [1945] = 1946, [1946] = 1945
}
local slots = {
    -- here are the slots on the treadmill, where the items will go through... you can add as many as you want...
    Position(32344, 32218, 4), Position(32345, 32218, 4), Position(32346, 32218, 4), Position(32347, 32218, 4),    Position(32348, 32218, 4),
    Position(32349, 32218, 4), Position(32350, 32218, 4)
}

local itemtable = {
    --here can have up to 100 items.. the chance can never be repeated, it must be from 1 to 100...
    -- insert the items respecting the order: [1], [2], [3], ... until the last [100]
    [1] = {id = 2390, chance = 100},
    [2] = {id = 2523, chance = 8},
    [3] = {id = 2646, chance = 10},
    [4] = {id = 2505, chance = 15},
    [5] = {id = 2469, chance = 20},
    [6] = {id = 2506, chance = 25},
    [7] = {id = 2471, chance = 30},
    [8] = {id = 2472, chance = 35},
    [9] = {id = 5184, chance = 40},
    [10] = {id = 5175, chance = 45},
    [11] = {id = 5185, chance = 50},
    [12] = {id = 2466, chance = 55},
    [13] = {id = 2195, chance = 60},
    [14] = {id = 5270, chance = 80},
    [15] = {id = 5181, chance = 65},
    [16] = {id = 2488, chance = 70},
    [17] = {id = 2477, chance = 75},
    [18] = {id = 5226, chance = 82}
}

local roulette = Action()

local function ender(cid, position)
    local player = Player(cid)
    local posicaofim = Position(32347, 32218, 4) -- HERE WILL APPEAR THE EFFECT, which defines the item that the player won
    local item = Tile(posicaofim):getTopDownItem()
    if item then
        local itemId = item:getId()
        posicaofim:sendMagicEffect(19)
        player:addItem(itemId, 1)
        player:say("You win a " ..item:getName().. "!")
    end
    local alavanca = Tile(position):getTopDownItem()
    if alavanca then
        alavanca:setActionId(18562) -- here comes the old actionid, to allow a next move...
    end
    if itemId == 2390 or itemId == 5185 or itemId == 2471 or itemId == 2466 or itemId == 2646 then --check if it is the ID of the LEGENDARY item
        Game.broadcastMessage('O player ' ..player:getName().. ' ganhou ' ..item:getName().. ' !', MESSAGE_STATUS_WARNING) -- if it's a rare item send it on the broadcast
       
        for _, pid in ipairs(getPlayersOnline()) do
            if pid ~= cid then
                pid:say("O player "..player:getName().." ganhou "..item:getName().."", TALKTYPE_MONSTER_SAY) -- if not legendary, send a regular message
            end
        end
    end
end

local function delay(position, aux)
    local item = Tile(position):getTopDownItem()
    if item then
        local slot = aux + 1
        item:moveTo(slots[slot])
    end  
end

local function exec(cid)
    --calculate a chance and assign an item
    local rand = math.random(1, 100)
    local aux, memo = 0, 0
    if rand >= 1 then
        for i = 1, #itemtable do
            local randitemid = itemtable[i].id
            local randitemchance = itemtable[i].chance
            if rand >= randitemchance then
                aux = aux + 1
                memo = randitemchance
            end
           
        end
    end
    -- Step one: Create an item in the first SLOT, and delete if there is an item in the last slot.
    print("aux = " .. aux)
    Game.createItem(itemtable[aux].id, 1, slots[1])
    slots[1]:sendMagicEffect(CONST_ME_POFF)
    local item = Tile(slots[#slots]):getTopDownItem()
    if item then
        item:remove()
    end
    --Step two: Move items to the next slot in all slots 1 to 12 for 2 > 13
    local maxslot = #slots-1
    local lastloop = 0
    for i = maxslot, 1, -1 do
       
        addEvent(delay, 1*1*900, slots[i], i)
    end
end

function roulette.onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    if not player then
        return false
    end
    if not player:removeItem(5197, 1) then -- TO PLAY the player must have item 5197, which represents a ticket sold in the store or at an npc....
        return false
    end
   
    item:transform(decayItems[item.itemid])
    item:decay()  
    --change the item's actionid to not allow running two instances
    item:setActionId(18563)
   
    local segundos = 15
    local loopsize = segundos*2
   
    for i = 1, loopsize do
        addEvent(exec, 1*i*500, cid.uid)
    end
    addEvent(ender, (1*loopsize*500)+1000, cid.uid, fromPosition)
   
    return true
end

roulette:aid(18562)
roulette:register()
 
Back
Top