• 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 addEvent erro

Hugo Patriota

Atzosh.com
Joined
Nov 23, 2016
Messages
29
Solutions
1
Reaction score
2
hi guys
The script is working perfectly, but the server returns me this error in the console, and I would like to remove it

Lua Script Error: [Spell Interface]
in callback: data/spells/scripts/pbot/utevo mort.lua:eek:nTargetTile
(Unknown scriptfile)
LuaScriptInterface::luaAddEvent(). Argument #3 is unsafe
stack traceback:
[C]: in function 'addEvent'
data/spells/scripts/pbot/utevo mort.lua:24: in function <data/spells/scr
ipts/pbot/utevo mort.lua:19>
[C]: at 0x013f90f5a0


http://pastebin.com/sh3BZ68S
 
Solution
To be honest, no idea what your script was trying to do. But anway you were sending an unsafe argument (in this case creature) on the addevent. You can either fix it manually which i prefer or let the server handle it by enable it throug config:
Code:
convertUnsafeScripts = true

Here is what i would done:

Lua:
local config = {
    monsterName = "Headcaptor",
    manaCost = 2000,
    timeRemoval = 5
}

local function removeMonster(id)
    local monster = Monster(id)
    if monster then
        monster:getPosition():sendMagicEffect(CONST_ME_POFF)
        monster:remove()
    end
end

function onTargetTile(creature, position)
    local monster = Game.createMonster(config.monsterName, position)
    if monster then...
You may not pass userdata to addEvent, because it poses a security risk and could crash the server. The server is automatically converting your userdata into numbers (e.g. player object into cid), to prevent such a thing from happening, hence why your code is still working. Instead of passing the "creature" to your callback, you should pass the "cid" and use "Creature(cid)" in the callback.
 
To be honest, no idea what your script was trying to do. But anway you were sending an unsafe argument (in this case creature) on the addevent. You can either fix it manually which i prefer or let the server handle it by enable it throug config:
Code:
convertUnsafeScripts = true

Here is what i would done:

Lua:
local config = {
    monsterName = "Headcaptor",
    manaCost = 2000,
    timeRemoval = 5
}

local function removeMonster(id)
    local monster = Monster(id)
    if monster then
        monster:getPosition():sendMagicEffect(CONST_ME_POFF)
        monster:remove()
    end
end

function onTargetTile(creature, position)
    local monster = Game.createMonster(config.monsterName, position)
    if monster then
        monster:setMaster(creature)
        creature:say("Utevo Mort", TALKTYPE_MONSTER_SAY)
        creature:addMana(config.manaCost, true)
        addEvent(removeMonster, config.timeRemoval * 1000, monster:getId())
    end
end

local area = {
    {1, 0, 1},
    {0, 3, 0},
    {1, 0, 1}
}

local combat = Combat()
combat:setArea(createCombatArea(area))
combat:setCallback(CALLBACK_PARAM_TARGETTILE, "onTargetTile")

function onCastSpell(creature, variant, isHotkey)
    return combat:execute(creature, variant)
end

Next time please post tfs version aswell.
 
Last edited by a moderator:
Solution
You may not pass userdata to addEvent, because it poses a security risk and could crash the server. The server is automatically converting your userdata into numbers (e.g. player object into cid), to prevent such a thing from happening, hence why your code is still working. Instead of passing the "creature" to your callback, you should pass the "cid" and use "Creature(cid)" in the callback.
I know everyone says it poses a security risk and can crash the server, but could you explain more?
Like.. what is the security risk, and why does it crash the server?
I'm just curious. :(
 
I know everyone says it poses a security risk and can crash the server, but could you explain more?
Like.. what is the security risk, and why does it crash the server?
I'm just curious. :(
A userdata is referencing to a memory address, and if this userdata is being erased (e.g. player doesn't exist anymore) it will reference to an invalid address and this may lead to a crash. (Or at least, something like this, not my area of expertise.)
 
To be honest, no idea what your script was trying to do. But anway you were sending an unsafe argument (in this case creature) on the addevent. You can either fix it manually which i prefer or let the server handle it by enable it throug config:
Code:
convertUnsafeScripts = true

Here is what i would done:

Code:
local config = {
    monsterName = "Headcaptor",
    manaCost = 2000,
    timeRemoval = 5
}

local function removeMonster(id)
    local monster = Monster(id)
    if monster then
        monster:getPosition():sendMagicEffect(CONST_ME_POFF)
        monster:remove()
    end
end

function onTargetTile(creature, position)
    local monster = Game.createMonster(config.monsterName, position)
    if monster then
        monster:setMaster(creature)
        creature:say("Utevo Mort", TALKTYPE_MONSTER_SAY)
        creature:addMana(config.manaCost, true)
        addEvent(removeMonster, config.timeRemoval * 1000, monster:getId())
    end
end

local area = {
    {1, 0, 1},
    {0, 3, 0},
    {1, 0, 1}
}

local combat = Combat()
combat:setArea(createCombatArea(area))
combat:setCallback(CALLBACK_PARAM_TARGETTILE, "onTargetTile")

function onCastSpell(creature, variant, isHotkey)
    return combat:execute(creature, variant)
end

Next time please post tfs version aswell.
sorry tfs 1.3
I wanted to leave my thanks here, the script worked perfectly and without errors, thanks !!!
 
I know everyone says it poses a security risk and can crash the server, but could you explain more?
Like.. what is the security risk, and why does it crash the server?
I'm just curious. :(

A way to see it crash;
Lua:
function func(player)
    print(player:getPosition())
end

local player = Player(cid)
addEvent(func, 5000, player)
player:remove()

Add that into a talkactions script, the player will be stored in your memory, after 5 sec the script will try to remove the player pointer, but since we removed the player there is nothing to remove = dangling pointer = crash.
A userdata value is not ment to be saved, that is what the creature id is for;
Lua:
function func(cid)
    local player = Player(cid)
    if not player then
        return false
    end

    print(player:getPosition())
end

You can find a bunch of info about this by doing a quick google serach but thats the basics of it.
 
A way to see it crash;
Lua:
function func(player)
    print(player:getPosition())
end

local player = Player(cid)
addEvent(func, 5000, player)
player:remove()

Add that into a talkactions script, the player will be stored in your memory, after 5 sec the script will try to remove the player pointer, but since we removed the player there is nothing to remove = dangling pointer = crash.
A userdata value is not ment to be saved, that is what the creature id is for;
Lua:
function func(cid)
    local player = Player(cid)
    if not player then
        return false
    end

    print(player:getPosition())
end

You can find a bunch of info about this by doing a quick google serach but thats the basics of it.
Well I've never seen it crash before, but it'll definitely error with that. xD
I know whar you mean though.
 
Well I've never seen it crash before, but it'll definitely error with that. xD
I know whar you mean though.
it'll give an error in old versions, but it will crash in later versions since userdata is a raw memory value
trying to execute a method on a null object will result in a crash, rather then executing a function using an invalid creature id throws an error
that's why in addEvent callbacks you're supposed to pass creature id and reconstruct the object, and check if it has been reconstructed or not, and execute the rest of the code
 
Back
Top