• 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 Downgrading a 1.x script to 0.4

lolipop00

New Member
Joined
Oct 6, 2016
Messages
35
Reaction score
3
Hello everyone, i'm wondering if someone could downgrade this script for me to its compatible with TFS 0.4. I am sure many znote aac users that use 0.4 will also benifit from this :d

Lua:
function onThink(interval, lastExecution)
    local orderQuery = db.storeQuery([[
        SELECT
            MIN(`po`.`player_id`) AS `player_id`,
            `shop`.`id`,
            `shop`.`type`,
            `shop`.`itemid`,
            `shop`.`count`
        FROM `players_online` AS `po`
        INNER JOIN `players` AS `p`
            ON `po`.`player_id` = `p`.`id`
        INNER JOIN `znote_shop_orders` AS `shop`
            ON `p`.`account_id` = `shop`.`account_id`
        WHERE `shop`.`type` IN(1,5,6)
        GROUP BY `shop`.`id`
    ]])
    -- Detect if we got any results
    if orderQuery ~= false then
        local type_desc = {
            "itemids",
            "pending premium (skip)",
            "pending gender change (skip)",
            "pending character name change (skip)",
            "Outfit and addons",
            "Mounts"
        }
        repeat
            local player_id = result.getDataInt(orderQuery, 'player_id')
            local orderId = result.getDataInt(orderQuery, 'id')
            local orderType = result.getDataInt(orderQuery, 'type')
            local orderItemId = result.getDataInt(orderQuery, 'itemid')
            local orderCount = result.getDataInt(orderQuery, 'count')
            local served = false

            local player = Player(player_id)
            if player ~= nil then
                print("Processing shop order for: [".. player:getName() .."] type "..orderType..": ".. type_desc[orderType])
                local tile = Tile(player:getPosition())
                if tile ~= nil and tile:hasFlag(TILESTATE_PROTECTIONZONE) then
                    -- ORDER TYPE 1 (Regular item shop products)
                    if orderType == 1 then
                        served = true
                        -- Get wheight
                        if player:getFreeCapacity() >= ItemType(orderItemId):getWeight(orderCount) then
                            db.query("DELETE FROM `znote_shop_orders` WHERE `id` = " .. orderId .. ";")
                            player:addItem(orderItemId, orderCount)
                            player:sendTextMessage(MESSAGE_INFO_DESCR, "Congratulations! You have received " .. orderCount .. " x " .. ItemType(orderItemId):getName() .. "!")
                            print("Process complete. [".. player:getName() .."] has recieved " .. orderCount .. "x " .. ItemType(orderItemId):getName() .. ".")
                        else
                            player:sendTextMessage(MESSAGE_STATUS_WARNING, "You need more CAP to carry this order!")
                            print("Process canceled. [".. player:getName() .."] need more cap to carry " .. orderCount .. "x " .. ItemType(orderItemId):getName() .. ".")
                        end
                    end

                    -- ORDER TYPE 5 (Outfit and addon)
                    if orderType == 5 then
                        served = true
                        -- Make sure player don't already have this outfit and addon
                        if not player:hasOutfit(orderItemId, orderCount) then
                            db.query("DELETE FROM `znote_shop_orders` WHERE `id` = " .. orderId .. ";")
                            player:addOutfit(orderItemId)
                            player:addOutfitAddon(orderItemId, orderCount)
                            player:sendTextMessage(MESSAGE_INFO_DESCR, "Congratulations! You have received a new outfit!")
                            print("Process complete. [".. player:getName() .."] has recieved outfit: ["..orderItemId.."] with addon: ["..orderCount.."]")
                        else
                            player:sendTextMessage(MESSAGE_STATUS_WARNING, "You already have this outfit and addon!")
                            print("Process canceled. [".. player:getName() .."] already have outfit: ["..orderItemId.."] with addon: ["..orderCount.."].")
                        end
                    end

                    -- ORDER TYPE 6 (Mounts)
                    if orderType == 6 then
                        served = true
                        -- Make sure player don't already have this outfit and addon
                        if not player:hasMount(orderItemId) then
                            db.query("DELETE FROM `znote_shop_orders` WHERE `id` = " .. orderId .. ";")
                            player:addMount(orderItemId)
                            player:sendTextMessage(MESSAGE_INFO_DESCR, "Congratulations! You have received a new mount!")
                            print("Process complete. [".. player:getName() .."] has recieved mount: ["..orderItemId.."]")
                        else
                            player:sendTextMessage(MESSAGE_STATUS_WARNING, "You already have this mount!")
                            print("Process canceled. [".. player:getName() .."] already have mount: ["..orderItemId.."].")
                        end
                    end

                    -- If this order hasn't been processed yet (missing type handling?)
                    if not served then
                        print("Znote shop: Type ["..orderType.."] not properly processed. Missing Lua code?")
                    end
                else
                    player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have a pending shop order, please enter protection zone.')
                    print("Skipped one shop order. Reason: Player: [".. player:getName() .."] is not inside protection zone.")
                end
            else
                print("Skipped one shop order. Reason: Player with id [".. player_id .."] is not online.")
            end

        until not result.next(orderQuery)
        result.free(orderQuery)
    end
    return true
end
 
Solution
Hey dude, I know you don't usually do more of 0.4 scripts so I just want to thank you for taking your time, its working perfectly, exept for one thing. Outside of PZ it will not go say "You have a pending shop order.." and I do get errors
Lua:
[21:33:10.492] [Error - CreatureScript Interface]
[21:33:10.492] In a timer event called from:
[21:33:10.492] data/creaturescripts/scripts/znoteShop.lua:onLogin
[21:33:10.492] Description:
[21:33:10.492] (luaDoPlayerSendTextMessage) Player not found

[21:33:10.493] [Error - CreatureScript Interface]
[21:33:10.493] In a timer event called from:
[21:33:10.493] data/creaturescripts/scripts/znoteShop.lua:onLogin
[21:33:10.493] Description:
[21:33:10.493]...
Hey dude, I know you don't usually do more of 0.4 scripts so I just want to thank you for taking your time, its working perfectly, exept for one thing. Outside of PZ it will not go say "You have a pending shop order.." and I do get errors
Lua:
[21:33:10.492] [Error - CreatureScript Interface]
[21:33:10.492] In a timer event called from:
[21:33:10.492] data/creaturescripts/scripts/znoteShop.lua:onLogin
[21:33:10.492] Description:
[21:33:10.492] (luaDoPlayerSendTextMessage) Player not found

[21:33:10.493] [Error - CreatureScript Interface]
[21:33:10.493] In a timer event called from:
[21:33:10.493] data/creaturescripts/scripts/znoteShop.lua:onLogin
[21:33:10.493] Description:
[21:33:10.493] data/creaturescripts/scripts/znoteShop.lua:122: attempt to concatenate global 'playerName' (a nil value)
[21:33:10.493] stack traceback:
[21:33:10.493]  data/creaturescripts/scripts/znoteShop.lua:122: in function <data/creaturescripts/scripts/znoteShop.lua:6>
No problem. Those are easy to fix.
I'll fix it tonight when I get home from work.
 
Hey dude, I know you don't usually do more of 0.4 scripts so I just want to thank you for taking your time, its working perfectly, exept for one thing. Outside of PZ it will not go say "You have a pending shop order.." and I do get errors
Lua:
[21:33:10.492] [Error - CreatureScript Interface]
[21:33:10.492] In a timer event called from:
[21:33:10.492] data/creaturescripts/scripts/znoteShop.lua:onLogin
[21:33:10.492] Description:
[21:33:10.492] (luaDoPlayerSendTextMessage) Player not found

[21:33:10.493] [Error - CreatureScript Interface]
[21:33:10.493] In a timer event called from:
[21:33:10.493] data/creaturescripts/scripts/znoteShop.lua:onLogin
[21:33:10.493] Description:
[21:33:10.493] data/creaturescripts/scripts/znoteShop.lua:122: attempt to concatenate global 'playerName' (a nil value)
[21:33:10.493] stack traceback:
[21:33:10.493]  data/creaturescripts/scripts/znoteShop.lua:122: in function <data/creaturescripts/scripts/znoteShop.lua:6>
Lua:
-- Znote Shop v1.1 for Znote AAC on TFS 0.3.6+ Crying Damson.
-- converted from a talkaction to a looping addEvent within a onLogin script by Xikini

local onThink_delay = 15 -- in seconds (How often it will attempt to check for orders)

function znote_shop_onThink(cid, delay)
    if not isPlayer(cid) then
        return
    end
   
    local playerName = getCreatureName(cid)
    local accid = getAccountNumberByPlayerName(playerName)
   
    local type_desc = {
        "itemids",
        "pending premium (skip)",
        "pending gender change (skip)",
        "pending character name change (skip)",
        "Outfit and addons",
        "Mounts"
    }
   
    -- Create the query
    local orderQuery = db.storeQuery("SELECT `id`, `type`, `itemid`, `count` FROM `znote_shop_orders` WHERE `account_id` = " .. accid .. ";")
    local served = false
   
    -- Detect if we got any results
    if orderQuery ~= false then
        if getTileInfo(getCreaturePosition(cid)).protection then
            repeat
                -- Fetch order values
                local q_id = result.getDataInt(orderQuery, "id")
                local q_type = result.getDataInt(orderQuery, "type")
                local q_itemid = result.getDataInt(orderQuery, "itemid")
                local q_count = result.getDataInt(orderQuery, "count")
   
                local description = "Unknown or custom type"
                if type_desc[q_type] ~= nil then
                    description = type_desc[q_type]
                end
                print("Processing type "..q_type..": ".. description)
   
                -- ORDER TYPE 1 (Regular item shop products)
                if q_type == 1 then
                    served = true
                    -- Get weight
                    local playerCap = getPlayerFreeCap(cid)
                    local itemweight = getItemWeightById(q_itemid, q_count)
                    if playerCap >= itemweight and getTileInfo(getCreaturePosition(cid)).protection then
                        -- backpack check
                        local backpack = getPlayerSlotItem(cid, 3)
                        local gotItem = false
                        if(backpack and backpack.itemid > 0) then
                            local received = doAddContainerItem(getPlayerSlotItem(cid, 3).uid, q_itemid,q_count)
                            if(received ~= false) then
                                db.executeQuery("DELETE FROM `znote_shop_orders` WHERE `id` = " .. q_id .. ";")
                                doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Congratulations! You have received ".. q_count .." "..getItemNameById(q_itemid).."(s)!")
                                gotItem = true
                            end
                        end
   
                        if(not gotItem) then
                            doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, "You have no available space in backpack to receive that item.")
                        end
                    else
                        doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, "Need more CAP and Need ProtectZone!")
                    end
                end
                -- ORDER TYPE 5 (Outfit and addon)
                if q_type == 5 then
                    served = true
   
                    local itemid = q_itemid
                    local outfits = {}
   
                    if itemid > 1000 then
                        local first = math.floor(itemid/1000)
                        table.insert(outfits, first)
                        itemid = itemid - (first * 1000)
                    end
                    table.insert(outfits, itemid)
   
                    for _, outfitId in pairs(outfits) do
                        -- Make sure player don't already have this outfit and addon
                        if not canPlayerWearOutfit(cid, outfitId, q_count) then
                            db.executeQuery("DELETE FROM `znote_shop_orders` WHERE `id` = " .. q_id .. ";")
                            doPlayerAddOutfit(cid,outfitId,q_count)
                            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Congratulations! You have received a new outfit!")
                        else
                            doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, "You already have this outfit and addon!")
                        end
                    end
                end
   
                -- ORDER TYPE 6 (Mounts)
                if q_type == 6 then
                    served = true
                    -- Make sure player don't already have this outfit and addon
                    if not getPlayerMount(cid, q_itemid) then -- Failed to find a proper hasMount 0.3 function?
                        db.executeQuery("DELETE FROM `znote_shop_orders` WHERE `id` = " .. q_id .. ";")
                        doPlayerAddMount(cid, q_itemid)
                        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Congratulations! You have received a new mount!")
                    else
                        doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, "You already have this mount!")
                    end
                end
   
                -- Add custom order types here
                -- Type 1 is for itemids (Already coded here)
                -- Type 2 is for premium (Coded on web)
                -- Type 3 is for gender change (Coded on web)
                -- Type 4 is for character name change (Coded on web)
                -- Type 5 is for character outfit and addon (Already coded here)
                -- Type 6 is for mounts (Already coded here)
                -- Type 7 is for Instant house purchase (Not for TFS 0.3)
                -- So use type 8+ for custom stuff, like etc packages.
                -- if q_type == 8 then
                -- end
            until not result.next(orderQuery)
            result.free(orderQuery)
        else
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have a pending shop order, please enter protection zone.")
            print("Skipped one shop order. Reason: Player: [".. playerName .."] is not inside protection zone.")
        end
    end
   
    addEvent(znote_shop_onThink, delay * 1000, cid, delay)
end

function onLogin(cid)
    addEvent(znote_shop_onThink, 1000, cid, onThink_delay)
    return true
end
 
Solution
Lua:
-- Znote Shop v1.1 for Znote AAC on TFS 0.3.6+ Crying Damson.
-- converted from a talkaction to a looping addEvent within a onLogin script by Xikini

local onThink_delay = 15 -- in seconds (How often it will attempt to check for orders)

function znote_shop_onThink(cid, delay)
    if not isPlayer(cid) then
        return
    end
   
    local playerName = getCreatureName(cid)
    local accid = getAccountNumberByPlayerName(playerName)
   
    local type_desc = {
        "itemids",
        "pending premium (skip)",
        "pending gender change (skip)",
        "pending character name change (skip)",
        "Outfit and addons",
        "Mounts"
    }
   
    -- Create the query
    local orderQuery = db.storeQuery("SELECT `id`, `type`, `itemid`, `count` FROM `znote_shop_orders` WHERE `account_id` = " .. accid .. ";")
    local served = false
   
    -- Detect if we got any results
    if orderQuery ~= false then
        if getTileInfo(getCreaturePosition(cid), "protection") then
            repeat
                -- Fetch order values
                local q_id = result.getDataInt(orderQuery, "id")
                local q_type = result.getDataInt(orderQuery, "type")
                local q_itemid = result.getDataInt(orderQuery, "itemid")
                local q_count = result.getDataInt(orderQuery, "count")
   
                local description = "Unknown or custom type"
                if type_desc[q_type] ~= nil then
                    description = type_desc[q_type]
                end
                print("Processing type "..q_type..": ".. description)
   
                -- ORDER TYPE 1 (Regular item shop products)
                if q_type == 1 then
                    served = true
                    -- Get weight
                    local playerCap = getPlayerFreeCap(cid)
                    local itemweight = getItemWeightById(q_itemid, q_count)
                    if playerCap >= itemweight and getTileInfo(getCreaturePosition(cid)).protection then
                        -- backpack check
                        local backpack = getPlayerSlotItem(cid, 3)
                        local gotItem = false
                        if(backpack and backpack.itemid > 0) then
                            local received = doAddContainerItem(getPlayerSlotItem(cid, 3).uid, q_itemid,q_count)
                            if(received ~= false) then
                                db.executeQuery("DELETE FROM `znote_shop_orders` WHERE `id` = " .. q_id .. ";")
                                doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Congratulations! You have received ".. q_count .." "..getItemNameById(q_itemid).."(s)!")
                                gotItem = true
                            end
                        end
   
                        if(not gotItem) then
                            doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, "You have no available space in backpack to receive that item.")
                        end
                    else
                        doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, "Need more CAP and Need ProtectZone!")
                    end
                end
                -- ORDER TYPE 5 (Outfit and addon)
                if q_type == 5 then
                    served = true
   
                    local itemid = q_itemid
                    local outfits = {}
   
                    if itemid > 1000 then
                        local first = math.floor(itemid/1000)
                        table.insert(outfits, first)
                        itemid = itemid - (first * 1000)
                    end
                    table.insert(outfits, itemid)
   
                    for _, outfitId in pairs(outfits) do
                        -- Make sure player don't already have this outfit and addon
                        if not canPlayerWearOutfit(cid, outfitId, q_count) then
                            db.executeQuery("DELETE FROM `znote_shop_orders` WHERE `id` = " .. q_id .. ";")
                            doPlayerAddOutfit(cid,outfitId,q_count)
                            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Congratulations! You have received a new outfit!")
                        else
                            doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, "You already have this outfit and addon!")
                        end
                    end
                end
   
                -- ORDER TYPE 6 (Mounts)
                if q_type == 6 then
                    served = true
                    -- Make sure player don't already have this outfit and addon
                    if not getPlayerMount(cid, q_itemid) then -- Failed to find a proper hasMount 0.3 function?
                        db.executeQuery("DELETE FROM `znote_shop_orders` WHERE `id` = " .. q_id .. ";")
                        doPlayerAddMount(cid, q_itemid)
                        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Congratulations! You have received a new mount!")
                    else
                        doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, "You already have this mount!")
                    end
                end
   
                -- Add custom order types here
                -- Type 1 is for itemids (Already coded here)
                -- Type 2 is for premium (Coded on web)
                -- Type 3 is for gender change (Coded on web)
                -- Type 4 is for character name change (Coded on web)
                -- Type 5 is for character outfit and addon (Already coded here)
                -- Type 6 is for mounts (Already coded here)
                -- Type 7 is for Instant house purchase (Not for TFS 0.3)
                -- So use type 8+ for custom stuff, like etc packages.
                -- if q_type == 8 then
                -- end
            until not result.next(orderQuery)
            result.free(orderQuery)
        else
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have a pending shop order, please enter protection zone.")
            print("Skipped one shop order. Reason: Player: [".. playerName .."] is not inside protection zone.")
        end
    end
   
    addEvent(znote_shop_onThink, delay * 1000, cid, delay)
end

function onLogin(cid)
    addEvent(znote_shop_onThink, 1000, cid, onThink_delay)
    return true
end
Its just amazing, working perfectly, thank you :)
 
Back
Top