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

Solved What is wrong with this code?

Status
Not open for further replies.

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
72
I am trying to pass arguments to another function but I get this error:

Code:
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
data/spells/scripts/test.lua:6: attempt to index local 'oldTarget' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/spells/scripts/test.lua:6: in function <data/spells/scripts/test.lua:1>

Here is my code:

Lua:
function repeatF(cid, oldTarget, var)
    local player = Player(cid)
    if not player then
    print("Creature no longer exists")
        setPlayerStorageValue(oldTarget, 2105, -1)
        oldTarget:setNoMove(false)
        return
    end
    print("Creature still exists")
        local target = Player(cid):getTarget()
        local mana = Player(cid):getMana()
        if target and target == oldTarget then
            Player(cid):addMana(-50)
            addEvent(repeatF, 500)
        else
            Player(cid):setNoMove(false)
            oldTarget:setNoMove(false)
            setPlayerStorageValue(cid, 2104, -1)
            if oldTarget:isPlayer() then
                setPlayerStorageValue(oldTarget, 2105, -1)
            end
            return true
        end
end

function onCastSpell(cid, var)
    local oldTarget = Player(cid):getTarget()
    if not Player(cid) or not oldTarget  then
        return
    end
      
        Player(cid):setNoMove(true)
        oldTarget:setNoMove(true)

        repeatF(cid, oldTarget, var)
        return doCombat(cid, combat, var)
end

I am also wondering why it executes the player(cid) does not exist, when it does (line 3)

tfs 1.0
 
Solution
Lua:
function nextCall(argument1, argument2)
    print(argument1, argument2)
    addEvent(nextCall, 1000, argument1, argument2)
end

nextCall(argument1, argument2)

Thats how you do it, but you can't pass a userdata value in addEvent, so you have to use ex:
Lua:
nextCall(player:getId(), argument2)
If you logout and the script is set to run the next second you will either crash the server or (hopefully) get a nil error message.

I tried this method just now, unfortunately it still does not work. I feel like there is no helping it

Code Used:

Lua:
function repeatF(argument1)
    if not argument1 then
        return false
    end
    print("PRINT LINE --- ", argument1)
    addEvent(repeatF, 1000, argument1)
end

function...
I am trying to pass arguments to another function but I get this error:

Code:
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
data/spells/scripts/test.lua:6: attempt to index local 'oldTarget' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/spells/scripts/test.lua:6: in function <data/spells/scripts/test.lua:1>

Here is my code:

Lua:
function repeatF(cid, oldTarget, var)
    local player = Player(cid)
    if not player then
    print("Creature no longer exists")
        setPlayerStorageValue(oldTarget, 2105, -1)
        oldTarget:setNoMove(false)
        return
    end
    print("Creature still exists")
        local target = Player(cid):getTarget()
        local mana = Player(cid):getMana()
        if target and target == oldTarget then
            Player(cid):addMana(-50)
            addEvent(repeatF, 500)
        else
            Player(cid):setNoMove(false)
            oldTarget:setNoMove(false)
            setPlayerStorageValue(cid, 2104, -1)
            if oldTarget:isPlayer() then
                setPlayerStorageValue(oldTarget, 2105, -1)
            end
            return true
        end
end

function onCastSpell(cid, var)
    local oldTarget = Player(cid):getTarget()
    if not Player(cid) or not oldTarget  then
        return
    end
     
        Player(cid):setNoMove(true)
        oldTarget:setNoMove(true)

        repeatF(cid, oldTarget, var)
        return doCombat(cid, combat, var)
end

I am also wondering why it executes the player(cid) does not exist, when it does (line 3)

tfs 1.0

Lua:
    local player = Player(cid)
    if not player then
        return false
    end

    local oldTarget = player:getTarget()
Fix those in your script.

Im not 100% but does getTarget return a userdata value?
Lua:
player:getTarget()

Use print(player:getTarget()) and see what it prints.

And even if it does you aren't checking if it's nil or not, so make sure to do that aswell.
 
Code:
addEvent(repeatF, 500)

You need to pass the arguments:
Code:
addEvent(repeatF, 500, cid, target, var)

It might not work as intended or not work at all when passing userdata.
 
Lua:
    local player = Player(cid)
    if not player then
        return false
    end

    local oldTarget = player:getTarget()
Fix those in your script.

Im not 100% but does getTarget return a userdata value?
Lua:
player:getTarget()

Use print(player:getTarget()) and see what it prints.

And even if it does you aren't checking if it's nil or not, so make sure to do that aswell.

Code:
addEvent(repeatF, 500)

You need to pass the arguments:
Code:
addEvent(repeatF, 500, cid, target, var)

It might not work as intended or not work at all when passing userdata.

I just tried to perform simple function with returning arguments but something isn't right;

Lua:
function repeatF(cid, player, var)
    if not player then
    print("Creature no longer exists")
        return false
    end
    print("Creature still exists")
            addEvent(repeatF, 1000, cid, player, var)

end

function onCastSpell(cid, var)
    local player = Player(cid)
  
        repeatF(cid, player, var)
    return doCombat(cid, combat, var)
end

result:

G94wVrD.png



And here is the second try, this time without arguments in addEvent:

Lua:
function repeatF(cid, player, var)
    if not player then
    print("Creature no longer exists")
        return false
    end
    print("Creature still exists")
            addEvent(repeatF, 1000)

end

function onCastSpell(cid, var)
    local player = Player(cid)
  
        repeatF(cid, player, var)
    return doCombat(cid, combat, var)
end

result:

Gdk0CF9.png


if I fix this, I will fix my issue

I am just thinking that there is a little mistake somewhere but I have not passed arguments before so I don't really know how you do that


-------- EDIT

Even something like this:

Lua:
function repeatF(cid)
    print("--NEW EVENT--")
    local player = Player(cid):getId()
    print("SECOND LINE ", player)
    if player == nil then
    print("Creature no longer exists")
        return false
    end
    print("third ", player)
    print("Creature still exists")

    addEvent(repeatF, 1000, cid)
end

function onCastSpell(cid, var)
    local player = Player(cid):getId()
    print("FIRST LINE ", player)
        repeatF(cid)
    return doCombat(cid, combat, var)
end

Result:

UEheRFm.png
 
Last edited:
I just tried to perform simple function with returning arguments but something isn't right;

Lua:
function repeatF(cid, player, var)
    if not player then
    print("Creature no longer exists")
        return false
    end
    print("Creature still exists")
            addEvent(repeatF, 1000, cid, player, var)

end

function onCastSpell(cid, var)
    local player = Player(cid)
 
        repeatF(cid, player, var)
    return doCombat(cid, combat, var)
end

result:

G94wVrD.png



And here is the second try, this time without arguments in addEvent:

Lua:
function repeatF(cid, player, var)
    if not player then
    print("Creature no longer exists")
        return false
    end
    print("Creature still exists")
            addEvent(repeatF, 1000)

end

function onCastSpell(cid, var)
    local player = Player(cid)
 
        repeatF(cid, player, var)
    return doCombat(cid, combat, var)
end

result:

Gdk0CF9.png


if I fix this, I will fix my issue

I am just thinking that there is a little mistake somewhere but I have not passed arguments before so I don't really know how you do that


-------- EDIT

Even something like this:

Lua:
function repeatF(cid)
    print("--NEW EVENT--")
    local player = Player(cid):getId()
    print("SECOND LINE ", player)
    if player == nil then
    print("Creature no longer exists")
        return false
    end
    print("third ", player)
    print("Creature still exists")

    addEvent(repeatF, 1000, cid)
end

function onCastSpell(cid, var)
    local player = Player(cid):getId()
    print("FIRST LINE ", player)
        repeatF(cid)
    return doCombat(cid, combat, var)
end

Result:

UEheRFm.png

Lua:
function nextCall(argument1, argument2)
    print(argument1, argument2)
    addEvent(nextCall, 1000, argument1, argument2)
end

nextCall(argument1, argument2)

Thats how you do it, but you can't pass a userdata value in addEvent, so you have to use ex:
Lua:
nextCall(player:getId(), argument2)
If you logout and the script is set to run the next second you will either crash the server or (hopefully) get a nil error message.
 
Lua:
function nextCall(argument1, argument2)
    print(argument1, argument2)
    addEvent(nextCall, 1000, argument1, argument2)
end

nextCall(argument1, argument2)

Thats how you do it, but you can't pass a userdata value in addEvent, so you have to use ex:
Lua:
nextCall(player:getId(), argument2)
If you logout and the script is set to run the next second you will either crash the server or (hopefully) get a nil error message.

I tried this method just now, unfortunately it still does not work. I feel like there is no helping it

Code Used:

Lua:
function repeatF(argument1)
    if not argument1 then
        return false
    end
    print("PRINT LINE --- ", argument1)
    addEvent(repeatF, 1000, argument1)
end

function onCastSpell(cid, var)
    local player = Player(cid)
    print("FIRST LINE ", player)
        repeatF(player:getId())
    return doCombat(cid, combat, var)
end

5ZbfmbA.png



edit -------------------------------

Also did with function inside the function, like this; (we don't have to pass arguments)

Lua:
function onCastSpell(cid, var)
    print("FIRST LINE ", player)
 
    function repeatF()
        local player = Player(cid)
        if not player then
            return false
        end
        print("PRINT LINE --- ", player)
        addEvent(repeatF, 1000)
    end
 
        repeatF()
    return doCombat(cid, combat, var)
end

but this time, the userdata was different every time the player variable was executed, not sure why

OOpyLJ4.png



EDIT2 ---------------------

Even simple addEvent has an issue with that:

Lua:
    addEvent(
        function()
        if not player then
            return false
        end
        print("PRINT LINE --- ", player)
        end, 2500
    )


^ It will execute, even if the player is offline

EDIT3 --------------------------------

Even the script from tutorial doesnt seem to work correctly, as the sendHelloMessage is being executed (printed in console) when player is offline

[How-to] Using addEvent()

EDIT4 - FIXED -----------------

AddEvent to send spectator msg

Apparently I meant to add addEvent in execution function, instead of executing another function!
 
Last edited:
Solution
I tried this method just now, unfortunately it still does not work. I feel like there is no helping it

Code Used:

Lua:
function repeatF(argument1)
    if not argument1 then
        return false
    end
    print("PRINT LINE --- ", argument1)
    addEvent(repeatF, 1000, argument1)
end

function onCastSpell(cid, var)
    local player = Player(cid)
    print("FIRST LINE ", player)
        repeatF(player:getId())
    return doCombat(cid, combat, var)
end

5ZbfmbA.png



edit -------------------------------

Also did with function inside the function, like this; (we don't have to pass arguments)

Lua:
function onCastSpell(cid, var)
    print("FIRST LINE ", player)
 
    function repeatF()
        local player = Player(cid)
        if not player then
            return false
        end
        print("PRINT LINE --- ", player)
        addEvent(repeatF, 1000)
    end
 
        repeatF()
    return doCombat(cid, combat, var)
end

but this time, the userdata was different every time the player variable was executed, not sure why

OOpyLJ4.png



EDIT2 ---------------------

Even simple addEvent has an issue with that:

Lua:
    addEvent(
        function()
        if not player then
            return false
        end
        print("PRINT LINE --- ", player)
        end, 2500
    )


^ It will execute, even if the player is offline

EDIT3 --------------------------------

Even the script from tutorial doesnt seem to work correctly, as the sendHelloMessage is being executed (printed in console) when player is offline

[How-to] Using addEvent()

EDIT4 - FIXED -----------------

AddEvent to send spectator msg

Apparently I meant to add addEvent in execution function, instead of executing another function!

Well the main problem is that you are printing out a userdata "id", they don't have to be the same;
Lua:
function repeatF(cid)
    if not Player(cid) then
        return false
    end

    print("PRINT LINE --- ", cid)
    addEvent(repeatF, 1000, cid)
end

function onCastSpell(cid, var)
    repeatF(cid)
    doCombat(cid, combat, var)

    return true
end

If you want the same value all the time you should use Player:getGuid() insted of cid.
And never return a function, return a boolean value insted.
 
And never return a function, return a boolean value insted.
it isn't returning a function, it's returning what the function returns.
which would make sense to make sure onCastSpell returns correctly if combat:execute(creature, variant) fails (bad creature or variant)
it's not bad practice, it's been standard in TFS for a long time, why do you think all the spells use the same style and use return in the official code?
 
it isn't returning a function, it's returning what the function returns.
which would make sense to make sure onCastSpell returns correctly if combat:execute(creature, variant) fails (bad creature or variant)
it's not bad practice, it's been standard in TFS for a long time, why do you think all the spells use the same style and use return in the official code?

If you know what you are doing sure, but you could mess things up.
Thats what I tell ppl, to avoid getting more issues.
 
yet in this case you're telling him to avoid the TFS code convention.
??

If you are new to Lua or any language you should avoid things like this.
You might not agree but thats my opinion.
 
If you are new to Lua or any language you should avoid things like this.

You shouldn't avoid what is confusing you, then that simply means that you're not doing anything that is challenging you, which leads to you remaining in a state where you're not developing any skills or whatnot, i.e you have reached the point where you have stopped learning
 
Last edited:
@tokenzz
I think what he's saying is analogous to: if you're new to snowboarding, don't start off by trying to do hard tricks on jumps, to avoid ending up in a hospital.
Seems like sound advice to me, but I get what you're saying, and it's ultimately up to personal preference and what ones goals are.
I've always been more experimental myself, but to each their own.
 
@tokenzz
I think what he's saying is analogous to: if you're new to snowboarding, don't start off by trying to do hard tricks on jumps, to avoid ending up in a hospital.
Seems like sound advice to me, but I get what you're saying, and it's ultimately up to personal preference and what ones goals are.
I've always been more experimental myself, but to each their own.

I disagree. The things you are comparing are the same thing as comparing apples to pears. Of course you need to have some kind of basic knowledge. In this case, this is super basic things and doesn't require any master mind 1337 pro professor in CS (computer science) to understand it
 
I disagree. The things you are comparing are the same thing as comparing apples to pears. Of course you need to have some kind of basic knowledge. In this case, this is super basic things and doesn't require any master mind 1337 pro professor in CS (computer science) to understand it

Still ppl who just started scripting should always try to start with basic things.
Do I need to add a return value to this function? It's better to return true insted of the function getting a nil.
This will just cause you to get more errors that you don't understand.

Im not saying you should never try or learn things like this, but @e.e is right, why try to start of with something hard insted of easy?
And if OP can't solve something like this ... well he should stick to learning the basics first.

I have said that it's my opinion, you may and can have another one.

Closing this thread since OP wrote that it's fixed (Solved - What is wrong with this code?)
If you have more questions about this you can send me a PM and ill open it for you.
 
  • Like
Reactions: e.e
Status
Not open for further replies.
Back
Top