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

Help with Shop delivery script - canary 2.6.1

Qbecky

New Member
Joined
Mar 11, 2023
Messages
29
Reaction score
1
GitHub
QbeckyX
Last edited:
You need to rework it so it's self registrable
Lua:
local globalevent = GlobalEvent("Shop")

function globalevent.onThink(interval)
   -- put code here
end

globalevent:register()

And make sure to replace legacy methods like doPlayerAddItem to proper equivalents (see TFS's: compat.lua or canary luascript.cpp)
 
Last edited:
use this one, the item goes straight to the store box

Lua:
-- ### CONFIG ###
-- message to player "type", if delivery of item debugs client, it can be because of undefinied type (type that does not exist in your server LUA)
SHOP_MSG_TYPE = MESSAGE_EVENT_ADVANCE
-- ### END OF CONFIG ###

local shop = GlobalEvent("shop")

function shop.onThink(interval)
    local resultId = db.storeQuery("SELECT * FROM z_ots_comunication")
    if resultId ~= false then
        repeat
            local transactionId = result.getDataInt(resultId, "id")
            local player = Player(result.getDataString(resultId, "name"))

            if player then
                local itemId = tonumber(result.getDataString(resultId, "param1"))
                local itemCount = tonumber(result.getDataString(resultId, "param2"))
                local containerId = tonumber(result.getDataString(resultId, "param3"))
                local containerItemsInsideCount = tonumber(result.getDataString(resultId, "param4"))
                local shopOfferType = result.getDataString(resultId, "param5")
                local shopOfferName = result.getDataString(resultId, "param6")

                local item = Game.createItem(itemid, count)
                if item then
                    return item:getUniqueId()
                end


                -- DELIVER ITEM
                if shopOfferType == 'item' then
                    local newItem = Game.createItem(itemId, itemCount)
                    --  item does not exist, wrong id OR count
                    if not newItem then
                        player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                        print('ERROR! Website Shop (' .. player:getName() .. ') - cannot create item - invalid item ID OR count - ITEM ID: ' .. itemId .. ', ITEM COUNT: ' .. itemCount)
                        return true
                    end

                    -- get player store inbox as container, so we can add item to it
                    local playerStoreInbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
                    -- cannot open Store Inbox, report problem
                    if not playerStoreInbox then
                        player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                        print('ERROR! Website Shop (' .. player:getName() .. ') - cannot open player "Store Inbox" - it is not supported in your server OR variable "CONST_SLOT_STORE_INBOX" is not definied in LUA')
                        return true
                    end
                    -- add container with items to Store Inbox
                    receivedItemStatus = playerStoreInbox:addItemEx(newItem)

                    if type(receivedItemStatus) == "number" and receivedItemStatus == RETURNVALUE_NOERROR then
                        player:sendTextMessage(SHOP_MSG_TYPE, 'You received ' .. shopOfferName .. ' from Website Shop. You can find your item in STORE INBOX (under EQ).')
                        db.asyncQuery("DELETE FROM `z_ots_comunication` WHERE `id` = " .. transactionId)
                        db.asyncQuery("UPDATE `z_shop_history_item` SET `trans_state`= 'realized', `trans_real`=" .. os.time() .. " WHERE `id` = " .. transactionId)
                    else
                        player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                        print('ERROR! Website Shop (' .. player:getName() .. ') - cannot add item to STORE INBOX - unknown reason, is it\'s size limited and it is full? - ITEM ID: ' .. itemId .. ', ITEM COUNT: ' .. itemCount)
                    end

                    -- DELIVER CONTAINER
                elseif shopOfferType == 'container' then
                    -- create empty container
                    local newContainer = Game.createItem(containerId, 1)
                    -- container item does not exist OR item is not Container
                    if not newContainerUID or not Container(newContainer.uid) then
                        player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                        print('ERROR! Website Shop (' .. player:getName() .. ') - cannot create container - invalid container ID - CONTAINER ID:' .. containerId)
                        return true
                    end

                    -- add items to container
                    for i = 1, containerItemsInsideCount do
                        -- create new item
                        local newItem = Game.createItem(itemId, itemCount)
                        --  item does not exist, wrong id OR count
                        if not newItem then
                            player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                            print('ERROR! Website Shop (' .. player:getName() .. ') - cannot create item - invalid item ID OR count - ITEM ID: ' .. itemId .. ', ITEM COUNT: ' .. itemCount)
                            return true
                        end

                        -- add item to container
                        local addItemToContainerResult = newContainer:addItemEx(newItem)
                        -- report error if it's not possible to add item to container
                        if type(addItemToContainerResult) ~= "number" or addItemToContainerResult ~= RETURNVALUE_NOERROR then
                            player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                            print('ERROR! Website Shop (' .. player:getName() .. ') - cannot add item to container - item is not pickable OR variable "RETURNVALUE_NOERROR" is not definied in LUA - ITEM ID: ' .. itemId .. ', ITEM COUNT: ' .. itemCount)
                            return true
                        end
                    end

                    -- get player store inbox as container, so we can add item to it
                    local playerStoreInbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
                    -- cannot open Store Inbox, report problem
                    if not playerStoreInbox then
                        player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                        print('ERROR! Website Shop (' .. player:getName() .. ') - cannot open player "Store Inbox" - it is not supported in your server OR variable "CONST_SLOT_STORE_INBOX" is not definied in LUA')
                        return true
                    end
                    -- add container with items to Store Inbox
                    receivedItemStatus = playerStoreInbox:addItemEx(newContainer)

                    if type(receivedItemStatus) == "number" and receivedItemStatus == RETURNVALUE_NOERROR then
                        player:sendTextMessage(SHOP_MSG_TYPE, 'You received ' .. shopOfferName .. ' from Website Shop. You can find your item in STORE INBOX (under EQ).')
                        db.asyncQuery("DELETE FROM `z_ots_comunication` WHERE `id` = " .. transactionId)
                        db.asyncQuery("UPDATE `z_shop_history_item` SET `trans_state`= 'realized', `trans_real`=" .. os.time() .. " WHERE `id` = " .. transactionId)
                    else
                        player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                        print('ERROR! Website Shop (' .. player:getName() .. ') - cannot add container with items to STORE INBOX - unknown reason, is it\'s size limited and it is full? - ITEM ID: ' .. itemId .. ', ITEM COUNT: ' .. itemCount .. ', CONTAINER ID:' .. containerId .. ', ITEMS IN CONTAINER COUNT:' .. containerItemsInsideCount)
                    end

                    -- DELIVER YOUR CUSTOM THINGS
                elseif shopOfferType == 'mything' then -- addon, mount etc.
                    -- HERE YOUR CODE
                end
            end
        until not result.next(resultId)
        result.free(resultId)
    end

    return true
end

shop:interval(30000) -- how often to randomize rain / weather
shop:register()
 
@jel
What is that for?
Lua:
local item = Game.createItem(itemid, count)
if item then
    return item:getUniqueId()
end

Anyway it only supports items and could be simplified to get rid of duplicated code, but anyway it's a good starting point:

Lua:
-- ### CONFIG ###
-- message to player "type", if delivery of item debugs client, it can be because of undefinied type (type that does not exist in your server LUA)
SHOP_MSG_TYPE = MESSAGE_EVENT_ADVANCE
-- ### END OF CONFIG ###

local shop = GlobalEvent("shop")

function shop.onThink(interval)
    local resultId = db.storeQuery("SELECT * FROM z_ots_comunication")
    if not resultId then
        return true
    end

    repeat
        local transactionId = result.getDataInt(resultId, "id")
        local player = Player(result.getDataString(resultId, "name"))

        if player then
            local itemId = tonumber(result.getDataString(resultId, "param1"))
            local itemCount = tonumber(result.getDataString(resultId, "param2"))
            local containerId = tonumber(result.getDataString(resultId, "param3"))
            local containerItemsInsideCount = tonumber(result.getDataString(resultId, "param4"))
            local shopOfferType = result.getDataString(resultId, "param5")
            local shopOfferName = result.getDataString(resultId, "param6")
            local receivedItemStatus = RETURNVALUE_NOERROR
            local newItem = nil

            if shopOfferType == 'item' then
                newItem = Game.createItem(itemId, itemCount)
                if not newItem then
                    player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                    print('ERROR! Website Shop (' .. player:getName() .. ') - cannot create item - invalid item ID OR count - ITEM ID: ' .. itemId .. ', ITEM COUNT: ' .. itemCount)
                    return true
                end
            elseif shopOfferType == 'container' then
                newItem = Game.createItem(containerId, 1)
                if not newItem or not Container(newItem.uid) then
                    player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                    print('ERROR! Website Shop (' .. player:getName() .. ') - cannot create container - invalid container ID - CONTAINER ID:' .. containerId)
                    return true
                end

                for i = 1, containerItemsInsideCount do
                    local item = Game.createItem(itemId, itemCount)
                    if not item then
                        player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                        print('ERROR! Website Shop (' .. player:getName() .. ') - cannot create item - invalid item ID OR count - ITEM ID: ' .. itemId .. ', ITEM COUNT: ' .. itemCount)
                        return true
                    end

                    local addItemToContainerResult = newItem:addItemEx(item)
                    if type(addItemToContainerResult) ~= "number" or addItemToContainerResult ~= RETURNVALUE_NOERROR then
                        player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                        print('ERROR! Website Shop (' .. player:getName() .. ') - cannot add item to container - item is not pickable OR variable "RETURNVALUE_NOERROR" is not definied in LUA - ITEM ID: ' .. itemId .. ', ITEM COUNT: ' .. itemCount)
                        return true
                    end
                end
            end

            local playerStoreInbox = player:getSlotItem(CONST_SLOT_STORE_INBOX)
            if not playerStoreInbox then
                player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                print('ERROR! Website Shop (' .. player:getName() .. ') - cannot open player "Store Inbox" - it is not supported in your server OR variable "CONST_SLOT_STORE_INBOX" is not definied in LUA')
                return true
            end

            receivedItemStatus = playerStoreInbox:addItemEx(newItem)
            if type(receivedItemStatus) == "number" and receivedItemStatus == RETURNVALUE_NOERROR then
                player:sendTextMessage(SHOP_MSG_TYPE, 'You received ' .. shopOfferName .. ' from Website Shop. You can find your item in STORE INBOX (under EQ).')
                db.asyncQuery("DELETE FROM `z_ots_comunication` WHERE `id` = " .. transactionId)
                db.asyncQuery("UPDATE `z_shop_history_item` SET `trans_state`= 'realized', `trans_real`=" .. os.time() .. " WHERE `id` = " .. transactionId)
            else
                player:sendTextMessage(SHOP_MSG_TYPE, 'Website Shop bugged. Contact with administrator! Error is visible in server console.')
                print('ERROR! Website Shop (' .. player:getName() .. ') - cannot add container with items to STORE INBOX - unknown reason, is it\'s size limited and it is full? - ITEM ID: ' .. itemId .. ', ITEM COUNT: ' .. itemCount .. ', CONTAINER ID:' .. containerId .. ', ITEMS IN CONTAINER COUNT:' .. containerItemsInsideCount)
            end
        end
    until not result.next(resultId)

    result.free(resultId)
    return true
end

shop:interval(30000) -- how often to randomize rain / weather
shop:register()
 
Lua:
receivedItemStatus = playerStoreInbox:addItemEx(newItem, INDEX_WHEREEVER, FLAG_NOLIMIT)
print(receivedItemStatus)
 
I just added print to see what’s the result of the function: addItemEx
So if you can, show what’s above this error.

If it gives no result try to debug that in VS, put breakpoint and see why it fails to add that item



Edit:
Resolved, canary uses different item ID's so it failed to create item as it was not crystal coins but some other random item

Family Feud Lol GIF by Steve Harvey
 
Last edited:
I just added print to see what’s the result of the function: addItemEx
So if you can, show what’s above this error.

If it gives no result try to debug that in VS, put breakpoint and see why it fails to add that item



Edit:
Resolved, canary uses different item ID's so it failed to create item as it was not crystal coints but some other random item

Family Feud Lol GIF by Steve Harvey
Thanks very much for helping fix it.
Happy Birthday Love GIF by LINE FRIENDS
 
Back
Top