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

exchange golds for item like lever consumption

2057623

Member
Joined
Jan 16, 2012
Messages
97
Reaction score
12
I have a code I tried to change the code to be able to use the lever I needed to have 5 units of item 18423, but it didn't work, can someone help me with this, the default code is working normally, but I just wanted this, and if I didn't have the item he couldn't use it. I use TFS 1.4x 10.98.
code is this.


Code:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

if not randomitems then
    
    randomitems = {}   
    
    function randomitems:saveLog(...)
        local message = '[%s] %s has found %s %s\n'       
        local file = io.open('data/logs/randomitems/' .. ... .. '.log', 'a')
        
        if not file then
            return
        end

        io.output(file)
        io.write(message:format(os.date('%d/%m/%Y %H:%M'), ...))
        io.close(file)
    end
    
    function randomitems:random(p, obj, exhaust, item)
        if not rawequal(type(obj), 'table') then
            return error('table of items not found.')
        end
                
        if not p:getGroup():getAccess() then
            
            -- double-click protect
            if obj.exhaust and obj.exhaust > os.time() then
                return p:getMoney() >= obj.coust and p:sendCancelMessage('Lever exhausted for ' .. obj.exhaust - os.time() ..' seconds.') or true
            end   
            
            -- the exhaust of x object is global for all players
            obj.exhaust = os.time() + (not exhaust and 2 or exhaust)
            
            if obj.onlypremium and not p:isPremium() then
                return p:say('Sorry, only premium players can use this lever.', TALKTYPE_MONSTER_SAY)
            end
            
            if not p:removeMoney(obj.coust) then       
                return p:say('Payment fail, you need ' .. obj.coust .. ' gold coins.', TALKTYPE_MONSTER_SAY)
            end
            
            if exhaust > 1 then
                item:transform(item.itemid + 1)
                
                addEvent(function()
                    item:transform(item.itemid - 1)
                end, ((obj.exhaust - os.time())-1) * 1000)
            end
            
        end
        
        -- this function is necessary to repeat the loop if the result was nil
        local function randomize()
        
            for _, it in ipairs(obj) do
                if it.chance>=100-(math.random()*100) then
                    local item = p:addItem(it.itemid, it.amount)
                    local name = not rawequal(type(item), 'table') and item:getName() or item[1]:getName()
                    
                    self:saveLog(p:getName(), it.amount, name)
                    p:save() -- [security] save player   
                    
                    if it.broadcast then
                        local msg = '[Legendary Levers] %s has found %s %s.'
                        
                        if not p:getGroup():getAccess() then   
                        
                            Game.broadcastMessage(msg:format(p:getName(), rawequal(it.amount, 1) and 'a' or it.amount, name .. '' .. (it.amount > 1 and 's' or '')), MESSAGE_EVENT_ADVANCE)
                        end
                    end     
                    
                    p:sendTextMessage(MESSAGE_INFO_DESCR, 'Congratulations, you have found ' .. (rawequal(it.amount, 1) and 'a' or it.amount) .. ' ' .. name .. '' .. (it.amount > 1 and 's.' or '.'))                                   
                    return not p:isInGhostMode() and p:getPosition():sendMagicEffect(it.broadcast and 7 or 15)
                end
            end
            
            -- repeat
            randomize()
        end
        
        -- called by self:random(...)
        randomize()
        
        return true
    end

else
    
    error('>> randomitems/lib.lua loading failed.')
end

second code
Code:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

local lever = {

    {itemid = 8851, amount = 1, chance = 5, broadcast = true},
    {itemid = 8300, amount = 1, chance = 12, broadcast = false},
    {itemid = 9971, amount = 50, chance = 25, broadcast = false},
    {itemid = 7253, amount = 1, chance = 65, broadcast = false},
}

lever.onlypremium = false
lever.coust = 10000


function onUse(player, item) return randomitems:random(player, lever, 2, item) end
 
Solution
Friend is still not removing, click on the lever receives the item, but does not remove the id 18423
LIB:
Lua:
-- Inspired by Legendary Lever System, developed by lyu
-- https://otland.net/threads/exchange-golds-for-item-like-lever-consumption.285774/

local config = {
  maxChance = 100,
}

local lever = {
  available = 1945, -- item id, available to use
  inUse = 1946, -- transforms lever into this id, during exhaustion time
  exhaustion = 1000, -- ms
}

local effects = {
  reward = CONST_ME_FIREWORK_RED,
  notPossible = CONST_ME_POFF,
}

local messageTypes = {
  rewardMessageType = MESSAGE_STATUS_CONSOLE_ORANGE,
  cancelMessageType = MESSAGE_STATUS_SMALL,
}

local messages = {
  messageReward = 'Congratulations! You have found...
Lua:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

if not randomitems then
    
    randomitems = {}   
    
    function randomitems:saveLog(...)
        local message = '[%s] %s has found %s %s\n'       
        local file = io.open('data/logs/randomitems/' .. ... .. '.log', 'a')
        
        if not file then
            return
        end

        io.output(file)
        io.write(message:format(os.date('%d/%m/%Y %H:%M'), ...))
        io.close(file)
    end
    
    function randomitems:random(p, obj, exhaust, item)
        if not rawequal(type(obj), 'table') then
            return error('table of items not found.')
        end


        if not p:hasItem(18423, 5) then
            return p:sendCancelMessage('You need 5 units of the required item to use this lever.')
        end
                
        if not p:getGroup():getAccess() then
            
            -- double-click protect
            if obj.exhaust and obj.exhaust > os.time() then
                return p:getMoney() >= obj.coust and p:sendCancelMessage('Lever exhausted for ' .. obj.exhaust - os.time() ..' seconds.') or true
            end   
            
            -- the exhaust of x object is global for all players
            obj.exhaust = os.time() + (not exhaust and 2 or exhaust)
            
            if obj.onlypremium and not p:isPremium() then
                return p:say('Sorry, only premium players can use this lever.', TALKTYPE_MONSTER_SAY)
            end
            
            if not p:removeMoney(obj.coust) then       
                return p:say('Payment fail, you need ' .. obj.coust .. ' gold coins.', TALKTYPE_MONSTER_SAY)
            end
            
            if exhaust > 1 then
                item:transform(item.itemid + 1)
                
                addEvent(function()
                    item:transform(item.itemid - 1)
                end, ((obj.exhaust - os.time())-1) * 1000)
            end
            
        end
        
        -- this function is necessary to repeat the loop if the result was nil
        local function randomize()
        
            for _, it in ipairs(obj) do
                if it.chance>=100-(math.random()*100) then
                    local item = p:addItem(it.itemid, it.amount)
                    local name = not rawequal(type(item), 'table') and item:getName() or item[1]:getName()
                    
                    self:saveLog(p:getName(), it.amount, name)
                    p:save() -- [security] save player   
                    
                    if it.broadcast then
                        local msg = '[Legendary Levers] %s has found %s %s.'
                        
                        if not p:getGroup():getAccess() then   
                        
                            Game.broadcastMessage(msg:format(p:getName(), rawequal(it.amount, 1) and 'a' or it.amount, name .. '' .. (it.amount > 1 and 's' or '')), MESSAGE_EVENT_ADVANCE)
                        end
                    end     
                    
                    p:sendTextMessage(MESSAGE_INFO_DESCR, 'Congratulations, you have found ' .. (rawequal(it.amount, 1) and 'a' or it.amount) .. ' ' .. name .. '' .. (it.amount > 1 and 's.' or '.'))                                   
                    return not p:isInGhostMode() and p:getPosition():sendMagicEffect(it.broadcast and 7 or 15)
                end
            end
            
            -- repeat
            randomize()
        end
        
        -- called by self:random(...)
        randomize()
        
        return true
    end

else
    
    error('>> randomitems/lib.lua loading failed.')
end

Lua:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

local lever = {

    {itemid = 8851, amount = 1, chance = 5, broadcast = true},
    {itemid = 8300, amount = 1, chance = 12, broadcast = false},
    {itemid = 9971, amount = 50, chance = 25, broadcast = false},
    {itemid = 7253, amount = 1, chance = 65, broadcast = false},
}

lever.onlypremium = false
lever.coust = 10000

function onUse(player, item)
    if not player:hasItem(18423, 5) then
        player:sendCancelMessage('You need 5 units of the required item to use this lever.')
        return true
    end

    return randomitems:random(player, lever, 2, item)
end
 
Lua:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

if not randomitems then
   
    randomitems = {}  
   
    function randomitems:saveLog(...)
        local message = '[%s] %s has found %s %s\n'      
        local file = io.open('data/logs/randomitems/' .. ... .. '.log', 'a')
       
        if not file then
            return
        end

        io.output(file)
        io.write(message:format(os.date('%d/%m/%Y %H:%M'), ...))
        io.close(file)
    end
   
    function randomitems:random(p, obj, exhaust, item)
        if not rawequal(type(obj), 'table') then
            return error('table of items not found.')
        end


        if not p:hasItem(18423, 5) then
            return p:sendCancelMessage('You need 5 units of the required item to use this lever.')
        end
               
        if not p:getGroup():getAccess() then
           
            -- double-click protect
            if obj.exhaust and obj.exhaust > os.time() then
                return p:getMoney() >= obj.coust and p:sendCancelMessage('Lever exhausted for ' .. obj.exhaust - os.time() ..' seconds.') or true
            end  
           
            -- the exhaust of x object is global for all players
            obj.exhaust = os.time() + (not exhaust and 2 or exhaust)
           
            if obj.onlypremium and not p:isPremium() then
                return p:say('Sorry, only premium players can use this lever.', TALKTYPE_MONSTER_SAY)
            end
           
            if not p:removeMoney(obj.coust) then      
                return p:say('Payment fail, you need ' .. obj.coust .. ' gold coins.', TALKTYPE_MONSTER_SAY)
            end
           
            if exhaust > 1 then
                item:transform(item.itemid + 1)
               
                addEvent(function()
                    item:transform(item.itemid - 1)
                end, ((obj.exhaust - os.time())-1) * 1000)
            end
           
        end
       
        -- this function is necessary to repeat the loop if the result was nil
        local function randomize()
       
            for _, it in ipairs(obj) do
                if it.chance>=100-(math.random()*100) then
                    local item = p:addItem(it.itemid, it.amount)
                    local name = not rawequal(type(item), 'table') and item:getName() or item[1]:getName()
                   
                    self:saveLog(p:getName(), it.amount, name)
                    p:save() -- [security] save player  
                   
                    if it.broadcast then
                        local msg = '[Legendary Levers] %s has found %s %s.'
                       
                        if not p:getGroup():getAccess() then  
                       
                            Game.broadcastMessage(msg:format(p:getName(), rawequal(it.amount, 1) and 'a' or it.amount, name .. '' .. (it.amount > 1 and 's' or '')), MESSAGE_EVENT_ADVANCE)
                        end
                    end    
                   
                    p:sendTextMessage(MESSAGE_INFO_DESCR, 'Congratulations, you have found ' .. (rawequal(it.amount, 1) and 'a' or it.amount) .. ' ' .. name .. '' .. (it.amount > 1 and 's.' or '.'))                                  
                    return not p:isInGhostMode() and p:getPosition():sendMagicEffect(it.broadcast and 7 or 15)
                end
            end
           
            -- repeat
            randomize()
        end
       
        -- called by self:random(...)
        randomize()
       
        return true
    end

else
   
    error('>> randomitems/lib.lua loading failed.')
end

Lua:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

local lever = {

    {itemid = 8851, amount = 1, chance = 5, broadcast = true},
    {itemid = 8300, amount = 1, chance = 12, broadcast = false},
    {itemid = 9971, amount = 50, chance = 25, broadcast = false},
    {itemid = 7253, amount = 1, chance = 65, broadcast = false},
}

lever.onlypremium = false
lever.coust = 10000

function onUse(player, item)
    if not player:hasItem(18423, 5) then
        player:sendCancelMessage('You need 5 units of the required item to use this lever.')
        return true
    end

    return randomitems:random(player, lever, 2, item)
end

Lua Script Error: [Action Interface]
data/actions/scripts/legendarylever/royalcrossbow.lua:eek:nUse
data/actions/scripts/legendarylever/royalcrossbow.lua:15: attempt to call method 'hasItem' (a nil value)
stack traceback:
[C]: in function 'hasItem'
data/actions/scripts/legendarylever/royalcrossbow.lua:15: in function <data/actions/scripts/legendarylever/royalcrossbow.lua:14>
 
Lua Script Error: [Action Interface]
data/actions/scripts/legendarylever/royalcrossbow.lua:eek:nUse
data/actions/scripts/legendarylever/royalcrossbow.lua:15: attempt to call method 'hasItem' (a nil value)
stack traceback:
[C]: in function 'hasItem'
data/actions/scripts/legendarylever/royalcrossbow.lua:15: in function <data/actions/scripts/legendarylever/royalcrossbow.lua:14>

player:getItemCount(itemId, count)
 
Lua Script Error: [Action Interface]
data/actions/scripts/legendarylever/royalcrossbow.lua:eek:nUse
data/actions/scripts/legendarylever/royalcrossbow.lua:15: attempt to call method 'hasItem' (a nil value)
stack traceback:
[C]: in function 'hasItem'
data/actions/scripts/legendarylever/royalcrossbow.lua:15: in function <data/actions/scripts/legendarylever/royalcrossbow.lua:14>
Lua:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

local lever = {

    {itemid = 8851, amount = 1, chance = 5, broadcast = true},
    {itemid = 8300, amount = 1, chance = 12, broadcast = false},
    {itemid = 9971, amount = 50, chance = 25, broadcast = false},
    {itemid = 7253, amount = 1, chance = 65, broadcast = false},
}

lever.onlypremium = false
lever.coust = 10000

function onUse(player, item)
    local requiredItemID = 18423
    local requiredItemCount = 5
    
    local itemCount = player:getItemCount(requiredItemID)
    if itemCount < requiredItemCount then
        player:sendCancelMessage('You need ' .. requiredItemCount .. ' units of the required item to use this lever.')
        return true
    end

    return randomitems:random(player, lever, 2, item)
end
 
Lua:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

local lever = {

    {itemid = 8851, amount = 1, chance = 5, broadcast = true},
    {itemid = 8300, amount = 1, chance = 12, broadcast = false},
    {itemid = 9971, amount = 50, chance = 25, broadcast = false},
    {itemid = 7253, amount = 1, chance = 65, broadcast = false},
}

lever.onlypremium = false
lever.coust = 10000

function onUse(player, item)
    local requiredItemID = 18423
    local requiredItemCount = 5
   
    local itemCount = player:getItemCount(requiredItemID)
    if itemCount < requiredItemCount then
        player:sendCancelMessage('You need ' .. requiredItemCount .. ' units of the required item to use this lever.')
        return true
    end

    return randomitems:random(player, lever, 2, item)
end
Lua Script Error: [Action Interface]
data/actions/scripts/legendarylever/royalcrossbow.lua:eek:nUse
data/actions/scripts/legendarylever/lib/lib.lua:26: attempt to call method 'hasItem' (a nil value)
stack traceback:
[C]: in function 'hasItem'
data/actions/scripts/legendarylever/lib/lib.lua:26: in function <data/actions/scripts/legendarylever/lib/lib.lua:20>
 
Lua Script Error: [Action Interface]
data/actions/scripts/legendarylever/royalcrossbow.lua:eek:nUse
data/actions/scripts/legendarylever/lib/lib.lua:26: attempt to call method 'hasItem' (a nil value)
stack traceback:
[C]: in function 'hasItem'
data/actions/scripts/legendarylever/lib/lib.lua:26: in function <data/actions/scripts/legendarylever/lib/lib.lua:20>

U didnt update ur post with his one
 
i think he changed in 1 script only, but it appears 2 times
Post automatically merged:


change
if not p:hasItem(18423, 5) then
to
if p:getItemCount(18423) < 5 then
Well, the error stopped, but the item is not removed from BP, so if the player does not have the item, he warns that he needs the item, but when he has the item, it is not removed from BP, he can use the lever as many times as he wants.

Show me ur current script
here the
file lib.lua

Code:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

if not randomitems then
    
    randomitems = {}   
    
    function randomitems:saveLog(...)
        local message = '[%s] %s has found %s %s\n'       
        local file = io.open('data/logs/randomitems/' .. ... .. '.log', 'a')
        
        if not file then
            return
        end

        io.output(file)
        io.write(message:format(os.date('%d/%m/%Y %H:%M'), ...))
        io.close(file)
    end
    
    function randomitems:random(p, obj, exhaust, item)
        if not rawequal(type(obj), 'table') then
            return error('table of items not found.')
        end


        if p:getItemCount(18423) < 5 then
            return p:sendCancelMessage('You need 5 units of the required item to use this lever.')
        end
                
        if not p:getGroup():getAccess() then
            
            -- double-click protect
            if obj.exhaust and obj.exhaust > os.time() then
                return p:getMoney() >= obj.coust and p:sendCancelMessage('Lever exhausted for ' .. obj.exhaust - os.time() ..' seconds.') or true
            end   
            
            -- the exhaust of x object is global for all players
            obj.exhaust = os.time() + (not exhaust and 2 or exhaust)
            
            if obj.onlypremium and not p:isPremium() then
                return p:say('Sorry, only premium players can use this lever.', TALKTYPE_MONSTER_SAY)
            end
            
            if not p:removeMoney(obj.coust) then       
                return p:say('Payment fail, you need ' .. obj.coust .. ' gold coins.', TALKTYPE_MONSTER_SAY)
            end
            
            if exhaust > 1 then
                item:transform(item.itemid + 1)
                
                addEvent(function()
                    item:transform(item.itemid - 1)
                end, ((obj.exhaust - os.time())-1) * 1000)
            end
            
        end
        
        -- this function is necessary to repeat the loop if the result was nil
        local function randomize()
        
            for _, it in ipairs(obj) do
                if it.chance>=100-(math.random()*100) then
                    local item = p:addItem(it.itemid, it.amount)
                    local name = not rawequal(type(item), 'table') and item:getName() or item[1]:getName()
                    
                    self:saveLog(p:getName(), it.amount, name)
                    p:save() -- [security] save player   
                    
                    if it.broadcast then
                        local msg = '[Legendary Levers] %s has found %s %s.'
                        
                        if not p:getGroup():getAccess() then   
                        
                            Game.broadcastMessage(msg:format(p:getName(), rawequal(it.amount, 1) and 'a' or it.amount, name .. '' .. (it.amount > 1 and 's' or '')), MESSAGE_EVENT_ADVANCE)
                        end
                    end     
                    
                    p:sendTextMessage(MESSAGE_INFO_DESCR, 'Congratulations, you have found ' .. (rawequal(it.amount, 1) and 'a' or it.amount) .. ' ' .. name .. '' .. (it.amount > 1 and 's.' or '.'))                                   
                    return not p:isInGhostMode() and p:getPosition():sendMagicEffect(it.broadcast and 7 or 15)
                end
            end
            
            -- repeat
            randomize()
        end
        
        -- called by self:random(...)
        randomize()
        
        return true
    end

else
    
    error('>> randomitems/lib.lua loading failed.')
end


File royalcrossbow.lua
Code:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

local lever = {

    {itemid = 8851, amount = 1, chance = 5, broadcast = true},
    {itemid = 8300, amount = 1, chance = 12, broadcast = false},
    {itemid = 9971, amount = 50, chance = 25, broadcast = false},
    {itemid = 7253, amount = 1, chance = 65, broadcast = false},
}

lever.onlypremium = false
lever.coust = 10000

function onUse(player, item)
    local requiredItemID = 18423
    local requiredItemCount = 5
    
    local itemCount = player:getItemCount(requiredItemID)
    if itemCount < requiredItemCount then
        player:sendCancelMessage('You need ' .. requiredItemCount .. ' units of the required item to use this lever.')
        return true
    end

    return randomitems:random(player, lever, 2, item)
end
 
Lua:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

if not randomitems then
    randomitems = {}  
   
    function randomitems:saveLog(...)
        local message = '[%s] %s has found %s %s\n'      
        local file = io.open('data/logs/randomitems/' .. ... .. '.log', 'a')
       
        if not file then
            return
        end

        io.output(file)
        io.write(message:format(os.date('%d/%m/%Y %H:%M'), ...))
        io.close(file)
    end
   
    function randomitems:random(p, obj, exhaust, item)
        if not rawequal(type(obj), 'table') then
            return error('table of items not found.')
        end

        if p:getItemCount(18423) < 5 then
            return p:sendCancelMessage('You need 5 units of the required item to use this lever.')
        end

        if not p:getGroup():getAccess() then
           
           
            if not p:removeMoney(obj.coust) then      
                return p:say('Payment fail, you need ' .. obj.coust .. ' gold coins.', TALKTYPE_MONSTER_SAY)
            end

            if exhaust > 1 then
                item:transform(item.itemid + 1)
               
                addEvent(function()
                    item:transform(item.itemid - 1)
                end, ((obj.exhaust - os.time()) - 1) * 1000)
            end
        end

        -- this function is necessary to repeat the loop if the result was nil
        local function randomize()
            for _, it in ipairs(obj) do
                if it.chance >= 100 - (math.random() * 100) then
                    local item = p:addItem(it.itemid, it.amount)
                    local name = not rawequal(type(item), 'table') and item:getName() or item[1]:getName()
                   
                    self:saveLog(p:getName(), it.amount, name)
                    p:save() -- [security] save player  
                   
                    if it.broadcast then
                        local msg = '[Legendary Levers] %s has found %s %s.'
                       
                        if not p:getGroup():getAccess() then  
                            Game.broadcastMessage(msg:format(p:getName(), rawequal(it.amount, 1) and 'a' or it.amount, name .. '' .. (it.amount > 1 and 's' or '')), MESSAGE_EVENT_ADVANCE)
                        end
                    end    

                    p:sendTextMessage(MESSAGE_INFO_DESCR, 'Congratulations, you have found ' .. (rawequal(it.amount, 1) and 'a' or it.amount) .. ' ' .. name .. '' .. (it.amount > 1 and 's.' or '.'))                                  
                     p:removeItem(item:getPosition(), 5) -- Remove 5 units of the required item
                    return not p:isInGhostMode() and p:getPosition():sendMagicEffect(it.broadcast and 7 or 15)
                end
            end
           
            -- repeat
            randomize()
        end
       
        -- called by self:random(...)
        randomize()
       
        return true
    end

else
    error('>> randomitems/lib.lua loading failed.')
end
 
Lua:
-- Legendary Lever was developed by lyu (Legendary Lever now is public)

if not randomitems then
    randomitems = {} 
  
    function randomitems:saveLog(...)
        local message = '[%s] %s has found %s %s\n'     
        local file = io.open('data/logs/randomitems/' .. ... .. '.log', 'a')
      
        if not file then
            return
        end

        io.output(file)
        io.write(message:format(os.date('%d/%m/%Y %H:%M'), ...))
        io.close(file)
    end
  
    function randomitems:random(p, obj, exhaust, item)
        if not rawequal(type(obj), 'table') then
            return error('table of items not found.')
        end

        if p:getItemCount(18423) < 5 then
            return p:sendCancelMessage('You need 5 units of the required item to use this lever.')
        end

        if not p:getGroup():getAccess() then
          
          
            if not p:removeMoney(obj.coust) then     
                return p:say('Payment fail, you need ' .. obj.coust .. ' gold coins.', TALKTYPE_MONSTER_SAY)
            end

            if exhaust > 1 then
                item:transform(item.itemid + 1)
              
                addEvent(function()
                    item:transform(item.itemid - 1)
                end, ((obj.exhaust - os.time()) - 1) * 1000)
            end
        end

        -- this function is necessary to repeat the loop if the result was nil
        local function randomize()
            for _, it in ipairs(obj) do
                if it.chance >= 100 - (math.random() * 100) then
                    local item = p:addItem(it.itemid, it.amount)
                    local name = not rawequal(type(item), 'table') and item:getName() or item[1]:getName()
                  
                    self:saveLog(p:getName(), it.amount, name)
                    p:save() -- [security] save player 
                  
                    if it.broadcast then
                        local msg = '[Legendary Levers] %s has found %s %s.'
                      
                        if not p:getGroup():getAccess() then 
                            Game.broadcastMessage(msg:format(p:getName(), rawequal(it.amount, 1) and 'a' or it.amount, name .. '' .. (it.amount > 1 and 's' or '')), MESSAGE_EVENT_ADVANCE)
                        end
                    end   

                    p:sendTextMessage(MESSAGE_INFO_DESCR, 'Congratulations, you have found ' .. (rawequal(it.amount, 1) and 'a' or it.amount) .. ' ' .. name .. '' .. (it.amount > 1 and 's.' or '.'))                                 
                     p:removeItem(item:getPosition(), 5) -- Remove 5 units of the required item
                    return not p:isInGhostMode() and p:getPosition():sendMagicEffect(it.broadcast and 7 or 15)
                end
            end
          
            -- repeat
            randomize()
        end
      
        -- called by self:random(...)
        randomize()
      
        return true
    end

else
    error('>> randomitems/lib.lua loading failed.')
end
Friend is still not removing, click on the lever receives the item, but does not remove the id 18423
 
Friend is still not removing, click on the lever receives the item, but does not remove the id 18423
LIB:
Lua:
-- Inspired by Legendary Lever System, developed by lyu
-- https://otland.net/threads/exchange-golds-for-item-like-lever-consumption.285774/

local config = {
  maxChance = 100,
}

local lever = {
  available = 1945, -- item id, available to use
  inUse = 1946, -- transforms lever into this id, during exhaustion time
  exhaustion = 1000, -- ms
}

local effects = {
  reward = CONST_ME_FIREWORK_RED,
  notPossible = CONST_ME_POFF,
}

local messageTypes = {
  rewardMessageType = MESSAGE_STATUS_CONSOLE_ORANGE,
  cancelMessageType = MESSAGE_STATUS_SMALL,
}

local messages = {
  messageReward = 'Congratulations! You have found ',
  broadcastReward = '[Legendary Levers] %s have found ', -- %s = player name
  playerNoMoney = 'Sorry, you need %d gold coins to pull this lever.', -- %d = cost
  playerNoItems = 'Sorry, you need %dx %s to pull this lever.', -- %d = amount, %s = item name
  playerNoPremium = 'Sorry, you need a premium account to pull this lever.',
  playerNoCap = 'You don\'t have enough capacity or space to receive your reward.',
}

local default = { -- default values in case any parameter is not specified
  requirements = {{id = 18423, amount = 5}}, -- you can have multiple requirements
  cost = 10000,
  -- reward
  minAmount = 1,
  maxAmount = 1,
}

local function notPossible(player,message)
  player:sendTextMessage(messageTypes.cancelMessageType, message)
  player:getPosition():sendMagicEffect(effects.notPossible)
end

local function getRandomReward(itemsList)
  local limit = 1000 -- just in case ^^
  local count = 0
  while count < limit do
    count = count + 1
    for id, info in pairs(itemsList) do
      local random = math.random(config.maxChance)
      if random <= info.chance then
        return id
      end
    end
  end
  debugPrint('Something went wrong! Probably maxChance is too much higher than items chance.')
end

local function checkRequirements(player,parameters)
  -- check premium
  if parameters.onlyPremium and not player:isPremium() then
    notPossible(player,messages.playerNoPremium)
    return false
  end
  -- check money
  if player:getMoney() < parameters.cost then
    notPossible(player,messages.playerNoMoney:format(parameters.cost))
    return false
  end
  -- check items
  for _, entry in pairs(parameters.requirements) do
    if player:getItemCount(entry.id) < entry.amount then
      notPossible(player,messages.playerNoItems:format(entry.amount,ItemType(entry.id):getName()))
      return false
    end
  end

  return true
end

local function removePlayerGoods(player,parameters)
  player:removeMoney(parameters.cost)
  for _, entry in pairs(parameters.requirements) do
    player:removeItem(entry.id,entry.amount)
  end
end

local function turnLever(itemLever)
  itemLever:transform(lever.inUse)
  addEvent(function()
    if itemLever then
      itemLever:transform(lever.available)
    end
  end,lever.exhaustion)
end

local function canUseLever(itemLever)
  return itemLever:getId() == lever.available
end

local function getRewardMessage(id,amount)
  local article = 'a'
  local plural = ''
  if amount > 1 then
    article = amount..'x'
    plural = 's'
  end
  return (article..' '..ItemType(id):getName()..".")
end

function onUseLegendaryLever(player,itemLever,itemsList,parameters)
  if not canUseLever(itemLever) then
    notPossible(player,'Please wait.')
    return true
  end

  parameters.requirements = parameters.requirements or default.requirements
  parameters.cost = parameters.cost or default.cost
  if checkRequirements(player,parameters) then
    turnLever(itemLever)
    -- get reward
    local rewardId = getRandomReward(itemsList)
    if not rewardId then
      -- it's not supposed to happen, but..
      notPossible(player,'Try again!')
      return true
    end
    -- deliver prize
    local prize = itemsList[rewardId]
    local count = math.random(prize.minAmount or default.minAmount, prize.maxAmount or default.maxAmount)
    if player:addItem(rewardId,count,false) then
      -- remove player goods
      removePlayerGoods(player,parameters)
      -- message
      local prizeMessage = getRewardMessage(rewardId,count)
      player:sendTextMessage(messageTypes.rewardMessageType,messages.messageReward..prizeMessage)
      player:getPosition():sendMagicEffect(effects.reward)
      -- broadcast
      if prize.broadcast then
        broadcastMessage(messages.broadcastReward:format(player:getName())..prizeMessage)
      end
    else
      notPossible(player,messages.playerNoCap)
    end
  end
  return true
end

Action:
Code:
local parameters = {
  requirements = {{id = 18423, amount = 5}}, -- you can have multiple requirements
  onlyPremium = true,
  cost = 10000,
}

local itemsList = {
  [8851] = {chance = 5, broadcast = true},
  [8300] = {chance = 12},
  [9971] = {minAmount = 40, maxAmount = 50, chance = 25},
  [7253] = {chance = 65},
}

local revAction = Action()

function revAction.onUse(player, itemLever)
  return onUseLegendaryLever(player,itemLever,itemsList,parameters)
end

revAction:aid(57587)
revAction:register()
 
Last edited:
Solution
Simple lever exchange script?
No, we must go deeper..

I'm honestly kind of impressed.
 
Simple lever exchange script?
No, we must go deeper..

I'm honestly kind of impressed.
Idk if it was ironic or not haha
The code is very similar, just made more easy to configure, imo, and more “step by step” to better understanding.
 
LIB:
Lua:
-- Inspired by Legendary Lever System, developed by lyu
-- https://otland.net/threads/exchange-golds-for-item-like-lever-consumption.285774/

local config = {
  maxChance = 100,
}

local lever = {
  available = 1945, -- item id, available to use
  inUse = 1946, -- transforms lever into this id, during exhaustion time
  exhaustion = 1000, -- ms
}

local effects = {
  reward = CONST_ME_FIREWORK_RED,
  notPossible = CONST_ME_POFF,
}

local messageTypes = {
  rewardMessageType = MESSAGE_STATUS_CONSOLE_ORANGE,
  cancelMessageType = MESSAGE_STATUS_SMALL,
}

local messages = {
  messageReward = 'Congratulations! You have found ',
  broadcastReward = '[Legendary Levers] %s have found ', -- %s = player name
  playerNoMoney = 'Sorry, you need %d gold coins to pull this lever.', -- %d = cost
  playerNoItems = 'Sorry, you need %dx %s to pull this lever.', -- %d = amount, %s = item name
  playerNoPremium = 'Sorry, you need a premium account to pull this lever.',
  playerNoCap = 'You don\'t have enough capacity or space to receive your reward.',
}

local default = { -- default values in case any parameter is not specified
  requirements = {{id = 18423, amount = 5}}, -- you can have multiple requirements
  cost = 10000,
  -- reward
  minAmount = 1,
  maxAmount = 1,
}

local function notPossible(player,message)
  player:sendTextMessage(messageTypes.cancelMessageType, message)
  player:getPosition():sendMagicEffect(effects.notPossible)
end

local function getRandomReward(itemsList)
  local limit = 1000 -- just in case ^^
  local count = 0
  while count < limit do
    count = count + 1
    for id, info in pairs(itemsList) do
      local random = math.random(config.maxChance)
      if random <= info.chance then
        Player("God"):mSay(count)
        return id
      end
    end
  end
  debugPrint('Something went wrong! Probably maxChance is too much higher than items chance.')
end

local function checkRequirements(player,parameters)
  -- check premium
  if parameters.onlyPremium and not player:isPremium() then
    notPossible(player,messages.playerNoPremium)
    return false
  end
  -- check money
  if player:getMoney() < parameters.cost then
    notPossible(player,messages.playerNoMoney:format(parameters.cost))
    return false
  end
  -- check items
  for _, entry in pairs(parameters.requirements) do
    if player:getItemCount(entry.id) < entry.amount then
      notPossible(player,messages.playerNoItems:format(entry.amount,ItemType(entry.id):getName()))
      return false
    end
  end

  return true
end

local function removePlayerGoods(player,parameters)
  player:removeMoney(parameters.cost)
  for _, entry in pairs(parameters.requirements) do
    player:removeItem(entry.id,entry.amount)
  end
end

local function turnLever(itemLever)
  itemLever:transform(lever.inUse)
  addEvent(function()
    if itemLever then
      itemLever:transform(lever.available)
    end
  end,lever.exhaustion)
end

local function canUseLever(itemLever)
  return itemLever:getId() == lever.available
end

local function getRewardMessage(id,amount)
  local article = 'a'
  local plural = ''
  if amount > 1 then
    article = amount..'x'
    plural = 's'
  end
  return (article..' '..ItemType(id):getName()..".")
end

function onUseLegendaryLever(player,itemLever,itemsList,parameters)
  if not canUseLever(itemLever) then
    notPossible(player,'Please wait.')
    return true
  end

  parameters.requirements = parameters.requirements or default.requirements
  parameters.cost = parameters.cost or default.cost
  if checkRequirements(player,parameters) then
    turnLever(itemLever)
    -- get reward
    local rewardId = getRandomReward(itemsList)
    if not rewardId then
      -- it's not supposed to happen, but..
      notPossible(player,'Try again!')
      return true
    end
    -- deliver prize
    local prize = itemsList[rewardId]
    local count = math.random(prize.minAmount or default.minAmount, prize.maxAmount or default.maxAmount)
    if player:addItem(rewardId,count,false) then
      -- remove player goods
      removePlayerGoods(player,parameters)
      -- message
      local prizeMessage = getRewardMessage(rewardId,count)
      player:sendTextMessage(messageTypes.rewardMessageType,messages.messageReward..prizeMessage)
      player:getPosition():sendMagicEffect(effects.reward)
      -- broadcast
      if prize.broadcast then
        broadcastMessage(messages.broadcastReward:format(player:getName())..prizeMessage)
      end
    else
      notPossible(player,messages.playerNoCap)
    end
  end
  return true
end

Action:
Code:
local parameters = {
  requirements = {{id = 18423, amount = 5}}, -- you can have multiple requirements
  onlyPremium = true,
  cost = 10000,
}

local itemsList = {
  [8851] = {chance = 5, broadcast = true},
  [8300] = {chance = 12},
  [9971] = {minAmount = 40, maxAmount = 50, chance = 25},
  [7253] = {chance = 65},
}

local revAction = Action()

function revAction.onUse(player, itemLever)
  return onUseLegendaryLever(player,itemLever,itemsList,parameters)
end

revAction:aid(57587)
revAction:register()
Well I added the action file in revscripts, but it's still giving an error.

Lua:
Lua Script Error: [Scripts Interface]
C:\Mit Server TFS 1.4\data\scripts\legendary.lua:callback
data/lib/Legendary.lua:54: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/lib/Legendary.lua:54: in function 'getRandomReward'
        data/lib/Legendary.lua:125: in function <data/lib/Legendary.lua:114>


It's hard to fix the code, if someone can or find it easier to create a code similar to this it can be too, if it's not abuse of everyone's kindness
 
Well I added the action file in revscripts, but it's still giving an error.

Lua:
Lua Script Error: [Scripts Interface]
C:\Mit Server TFS 1.4\data\scripts\legendary.lua:callback
data/lib/Legendary.lua:54: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/lib/Legendary.lua:54: in function 'getRandomReward'
        data/lib/Legendary.lua:125: in function <data/lib/Legendary.lua:114>


It's hard to fix the code, if someone can or find it easier to create a code similar to this it can be too, if it's not abuse of everyone's kindness
Just remove line 54 of lib file. I forgot to remove that
 
Back
Top