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

moving tfs1.1 script to 1.2 console error with null value

Verrine

Member
Joined
Mar 2, 2016
Messages
117
Reaction score
7
Hi I got a script for item upgrade but Ive got in console error like this:
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/upgrade.lua:onUse
data/actions/scripts/upgrade.lua:10: attempt to index global 'UpgradeHandler' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/actions/scripts/upgrade.lua:10: in function <data/actions/scripts/upgrade.lua:1>

here is this code fragment:

Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)

    if isCreature(itemEx.uid) then 

        return doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)

    end


    local obj = UpgradeHandler:new(itemEx)

    if(obj == false) then

        return doPlayerSendCancel(cid, UpgradeHandler.message.notupgradeable)

    end

dont knot why i changed cid to Player and added in the end of funcion after toPosition, isHotkey) and nothing happened still got same error in here. How is it possible. Im using TFS 1.2
can someone fix it?
 
Last edited:
local obj = UpgradeHandler:new(itemEx)
local obj = UpgradeHandler:new(itemEx.uid)
?
I really have no idea though for two reasons,
1 - I don't work with tfs 1.2,
2 - Not enough code to work with really
 
local obj = UpgradeHandler:new(itemEx)
local obj = UpgradeHandler:new(itemEx.uid)
?
I really have no idea though for two reasons,
1 - I don't work with tfs 1.2,
2 - Not enough code to work with really
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)

    if isCreature(itemEx.uid) then   

        return doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)

    end


    local obj = UpgradeHandler:new(itemEx.uid)

    if(obj == false) then

        return doPlayerSendCancel(cid, UpgradeHandler.message.notupgradeable)

    end


    local status = obj:refine(cid, item)

    if status == "success" then

        --doSendAnimatedText(toPosition, "Success!", COLOR_GREEN)

        doSendMagicEffect(toPosition, CONST_ME_MAGIC_GREEN)

    elseif status == "fail" then

        --doSendAnimatedText(toPosition, "Fail!", COLOR_RED)

        doSendMagicEffect(toPosition, CONST_ME_POFF)

    else

        doSendMagicEffect(toPosition, CONST_ME_POFF)

    end

    return true

end

and libs
Code:
--[[

    PERFECT UPGRADE SYSTEM
                2.0
               
    Criado por Oneshot
   
    É proibido a venda ou a cópia sem os devidos créditos desse script.
   
]]--

UpgradeHandler = {
    levels = {
        [1] = {100, false, false},
        [2] = {90, false, false},
        [3] = {75, false, false},
        [4] = {60, true, false},
        [5] = {45, true, false},
        [6] = {30, true, false},
        [7] = {25, true, false},
        [8] = {20, true, true},
        [9] = {15, true, true},
        [10] = {10, true, true}
    },
    broadcast = 7,
    attributes = {
        ["attack"] = 2,
        ["defense"] = 1,
        ["armor"] = 1
    },
    message = {
        console = "Trying to refine %s to level +%s with %s%% success rate.",
        success = "You have upgraded %s to level +%s",
        fail = "You have failed in upgrade of %s to level +%s",
        downgrade = "The upgrade level of %s has downgraded to +%s",
        erase = "The upgrade level of %s has been erased.",
        maxlevel = "The targeted %s is already on max upgrade level.",
        notupgradeable = "This item is not upgradeable.",
        broadcast = "The player %s was successful in upgrading %s to level +%s.\nCongratulations!!",
        invalidtool = "This is not a valid upgrade tool.",
        toolrange = "This upgrade tool can only be used in items with level between +%s and +%s"
    },
    tools = {
        [8306] = {range = {0, 10}, info = {chance = 0, removeable = false}},
    },
   
    isEquipment = function(self)
        local weaponType = self:getItemWeaponType()
        return ((weaponType > 0 and weaponType < 7) or self.item.armor ~= 0)
    end,
   
    setItemName = function(self, name)
        return doItemSetAttribute(self.item.uid, "name", name)
    end,
   
    chance = function(self)
        local chances = {}
        chances.upgrade = (self.levels[self.item.level + 1][1] or 100)
        chances.downgrade = (self.item.level * 5)
        chances.erase = (self.item.level * 3)

        return chances
    end
}

function UpgradeHandler:new(item)
    local obj, ret = {}
    obj.item = {}
   
    obj.item.level = 0
    obj.item.uid = item.uid
    for key, value in pairs(getItemInfo(item.itemid)) do
        obj.item[key] = value
    end
   
    ret = setmetatable(obj, {__index = function(self, index)
        if _G[index] then
            return (setmetatable({callback = _G[index]}, {__call = function(self, ...)
                return self.callback(item.uid, ...)
            end}))
        else
            return UpgradeHandler[index]
        end
    end})
   
    if ret:isEquipment() then
        ret:update()
        return ret
    end
    return false
end

function UpgradeHandler:update()
    self.item.level = (tonumber(self:getItemName():match("%+(%d+)")) or 0)
end

function UpgradeHandler:refine(uid, item)
    if not self.item then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.notupgradeable)
        return "miss"
    end   
   
    local tool = self.tools[item.itemid]
   
    if(tool == nil) then
        doPlayerSendTextMessage(uid, MESSAGE_EVENT_DEFAULT, self.message.invalidtool)
        return "miss"
    end
   
    if(self.item.level > #self.levels) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.maxlevel:format(self.item.name))
        return "miss"
    end
   
    if(self.item.level < tool.range[1] or self.item.level >= tool.range[2]) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.toolrange:format(unpack(tool.range)))
        return "miss"
    end
   
    local chance = (self:chance().upgrade + tool.info.chance)
    doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_BLUE, self.message.console:format(self.item.name, (self.item.level + 1), math.min(100, chance)))
   
    if(tool.info.removeable == false) then
        doRemoveItem(item.uid, 1)
    end
   
    if chance * 100 > math.random(1, 10000) then
        doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_ORANGE, self.message.success:format(self.item.name, (self.item.level + 1)))
        if (self.item.level + 1) >= self.broadcast then
            doBroadcastMessage(self.message.broadcast:format(getCreatureName(uid), self.item.name, (self.item.level + 1)))
        end
       
        self:setItemName((self.item.level > 0 and self:getItemName():gsub("%+(%d+)", "+".. (self.item.level + 1)) or (self:getItemName() .." +1")))
        for key, value in pairs(self.attributes) do
            if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                doItemSetAttribute(self.item.uid, key, (self.item.level > 0 and getItemAttribute(self.item.uid, key) or self.item[key]) + value)
            end
        end
        return "success"
    else
        if(self.levels[self.item.level][3] == true and (self:chance().erase * 100) > math.random(1, 10000)) then
            doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.erase:format(self.item.name))
            self:setItemName(self.item.name)
            for key, value in pairs(self.attributes) do
                if self.item[key] > 0 then
                    doItemSetAttribute(self.item.uid, key, self.item[key])
                end
            end
        elseif(self.levels[self.item.level][2] == true and (self:chance().downgrade * 100) > math.random(1, 10000)) then
            doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.downgrade:format(self.item.name, (self.item.level - 1)))
            self:setItemName((self.item.level == 1 and self.item.name or self:getItemName():gsub("%+(%d+)", "+".. (self.item.level - 1))))
            for key, value in pairs(self.attributes) do
                if getItemAttribute(self.item.uid, key) ~= nil or self.item[key] ~= 0 then
                    doItemSetAttribute(self.item.uid, key, (self.item[key] + value * (self.item.level - 1)))
                end
            end
        else
            doPlayerSendTextMessage(uid, MESSAGE_STATUS_CONSOLE_RED, self.message.fail:format(self.item.name, (self.item.level + 1)))
        end
        return "fail"
    end
end

after changing to uid nothing changed
 
onUse is changed in 1.2:
Code:
onUse(player, item, fromPosition, target, toPosition, isHotkey)
I dont have time to check anything else right now. I will come back later.
 
onUse is changed in 1.2:
Code:
onUse(player, item, fromPosition, target, toPosition, isHotkey)
I dont have time to check anything else right now. I will come back later.
I changed it before and nothing happened so I just pasted here original code becouse of same error with both onUse functions but yeah Ill save the new one onUse as you said. Okay ill wait for your response
 
Last edited:
Cid is not used in 1.2. Ctrl+f in your script and change all cid to player :)
On phone in bed so can't look to close on your code.
 
If a script uses cid as an argument then you can simply create a local variable.
For instance if the script was meant for 1.0 and you are using a 1.1 or 1.2 server you would update the arguments in the interface.
This
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
Would become this
Code:
function onUse(player, item, fromPosition, itemEx, toPosition)
Once you are done there make sure your compat.lua file has the appropriate functions which use cid as an argument.
Then you can simply create a local variable like so. This would be created right underneath the interface here is an example below.
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
     local cid = player:getId()
Now you won't have to update all of the functions this is just a simple work around to use code which wasn't exactly meant for the distribution you are working in.

You might be saying to yourself that the 1.2 onUse interface isn't the same as other interfaces well your right and wrong it is but it isn't. As long as the values being passed to the inteface's parameters are utilized appropriately it doesn't matter what their names are labeled as.
 
Last edited:
Back
Top