• 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 add some functions

44 Hunter

New Member
Joined
Dec 14, 2017
Messages
18
Reaction score
4
Could someone add some functions to my script? Are they:

I want to be able to add an actionID to the item.
I also need the items to be delivered inside a bag.
It is also necessary to set an actionID on the item if it is inside a bag.

Lua:
local configs = {
    uniqueIds = {
        [2001] = {storage = PlayerStorageKeys.AlawarsVault, receiveItem = {id = 2089, actionId = 4503}, magicEffect = 30, message = "The reward you chose was %s."},
    }
}

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local config = configs.uniqueIds[item.uid]
    if not config then
        return true
    end
    
    if player:getStorageValue(config.storage) >= 0 then
        player:sendCancelMessage("You have already received a reward.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return true
    end
    
    local item = Game.createItem(config.receiveItem.id, config.receiveItem.count)
    if player:addItemEx(item) ~= RETURNVALUE_NOERROR then
        player:sendCancelMessage("You don't have the capacity or space to pick up the item.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return true
    end
    
    player:setStorageValue(config.storage, 1)
    player:getPosition():sendMagicEffect(config.magicEffect)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, config.message:format(ItemType(config.receiveItem.id):getName()))
    return true
end
for k, v in pairs(configs.uniqueIds) do
    action:uid(k)
end
action:register()
 
Lua:
local configs = {
    uniqueIds = {
        [2001] = {storage = PlayerStorageKeys.AlawarsVault, receiveItem = {id = 2089, count = 1, actionId = 4503}, magicEffect = 30, message = "The reward you chose was %s."},
    }
}

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local config = configs.uniqueIds[item.uid]
    if not config then
        return true
    end
   
    if player:getStorageValue(config.storage) >= 0 then
        player:sendCancelMessage("You have already received a reward.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return true
    end
   
    local bag = Game.createItem(1987, 1)
    if not bag then
        return true
    end
   
    local item = Game.createItem(config.receiveItem.id, config.receiveItem.count)
    if not item then
        return true
    end
   
    item:setActionId(config.receiveItem.actionId)
    if bag:addItemEx(item) ~= RETURNVALUE_NOERROR then
        return true
    end
   
    if player:addItemEx(bag) ~= RETURNVALUE_NOERROR then
        player:sendCancelMessage("You don't have the capacity to pick up the bag.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return true
    end
   
    player:setStorageValue(config.storage, 1)
    player:getPosition():sendMagicEffect(config.magicEffect)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, config.message:format(ItemType(config.receiveItem.id):getName()))
    return true
end
for k, v in pairs(configs.uniqueIds) do
    action:uid(k)
end
action:register()
 
@Boy67 It works perfectly, but only if 1 item comes with AID inside the bag.
How to make more than 1 item come inside the bag? Some with other AID and some without any AID
 
Sure. Something like this (untested):
Lua:
local configs = {
    uniqueIds = {
        [2001] = {
            storage = PlayerStorageKeys.AlawarsVault, 
            items = {
                {id = 2089, count = 1, actionId = 4503},
                {id = 2160, count = 10},
            }, 
            magicEffect = 30, 
        },
    }
}

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local config = configs.uniqueIds[item.uid]
    if not config then
        return true
    end
    
    local message = "You have been rewarded with "
   
    if player:getStorageValue(config.storage) >= 0 then
        player:sendCancelMessage("You have already received a reward.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return true
    end
   
    local bag = Game.createItem(1987, 1)
    if not bag then
        return true
    end

    for i, itemdata in ipairs(config.items) do
        local item = Game.createItem(itemdata.id, itemdata.count)
        if item then    
            if itemdata.actionId then
                item:setActionId(itemdata.actionId)
            end
            
            if bag:addItemEx(item) ~= RETURNVALUE_NOERROR then
                break
            end
            
            local itemType = ItemType(itemdata.id)
            local itemArticle = itemType:getArticle()
            local itemName = itemType:getName()
            local itemPluralName = itemType:getPluralName()
            
            if count == 1 then
                message = message .. itemArticle .. " " .. itemName
            else
                message = message .. itemdata.count .. " " .. itemPluralName
            end
            
            message = message .. (i < #config.items and ", " or ".")
            
        end
    end
   
    if player:addItemEx(bag) ~= RETURNVALUE_NOERROR then
        player:sendCancelMessage("You don't have the capacity to pick up the bag.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return true
    end
   
    player:setStorageValue(config.storage, 1)
    player:getPosition():sendMagicEffect(config.magicEffect)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, message)
    return true
end
for k, v in pairs(configs.uniqueIds) do
    action:uid(k)
end
action:register()
 
@Boy67 Thank you very much, it worked perfectly.
Could you make a version, so that only 1 item can come, but with AID? In this case, it would be just one, without coming in a bag
 
So if it's only one item, its not in a bag?

Lua:
local configs = {
    uniqueIds = {
        [2001] = {
            storage = PlayerStorageKeys.AlawarsVault,
            items = {
                {id = 2089, count = 1, actionId = 4503},
                {id = 2160, count = 10},
            },
            magicEffect = 30,
        },
    }
}

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local config = configs.uniqueIds[item.uid]
    if not config then
        return true
    end
    
    local message = "You have been rewarded with "
  
    if player:getStorageValue(config.storage) >= 0 then
        player:sendCancelMessage("You have already received a reward.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return true
    end
    
    local bag = nil
    if #config.items > 1 then
        bag = Game.createItem(1987, 1)
        if not bag then
            return true
        end
    end

    for i, itemdata in ipairs(config.items) do
        local item = Game.createItem(itemdata.id, itemdata.count)
        if item then   
            if itemdata.actionId then
                item:setActionId(itemdata.actionId)
            end
            
            if bag then
                if bag:addItemEx(item) ~= RETURNVALUE_NOERROR then
                    break
                end
            else
                if player:addItemEx(item) ~= RETURNVALUE_NOERROR then
                    player:sendCancelMessage("You don't have the capacity to pick up the item.")
                    player:getPosition():sendMagicEffect(CONST_ME_POFF)
                    return true
                end
            end
            
            local itemType = ItemType(itemdata.id)
            local itemArticle = itemType:getArticle()
            local itemName = itemType:getName()
            local itemPluralName = itemType:getPluralName()
            
            if count == 1 then
                message = message .. itemArticle .. " " .. itemName
            else
                message = message .. itemdata.count .. " " .. itemPluralName
            end
            
            message = message .. (i < #config.items and ", " or ".")
            
        end
    end
    
    if bag then
        if player:addItemEx(bag) ~= RETURNVALUE_NOERROR then
            player:sendCancelMessage("You don't have the capacity to pick up the bag.")
            player:getPosition():sendMagicEffect(CONST_ME_POFF)
            return true
        end
    end
  
    player:setStorageValue(config.storage, 1)
    player:getPosition():sendMagicEffect(config.magicEffect)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, message)
    return true
end
for k, v in pairs(configs.uniqueIds) do
    action:uid(k)
end
action:register()


You may also have to check if the item is stackable or not, if so you will need to make another loop to add the item to bag/player
 
Last edited by a moderator:
Back
Top