• 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 stopEvent on logout.

elnelson

Lunaria World Dev
Joined
Jun 20, 2009
Messages
579
Solutions
2
Reaction score
58
Location
México
Hello otlanders, i still dont get how to stop an event. Can someone please give me a detailed explanation how to stop this event when a player logoff? My console get tons of messages trying to find the player logged

TFS 0.4
Lua:
local exhaust = createConditionObject(CONDITION_EXHAUST)
setConditionParam(exhaust, CONDITION_PARAM_TICKS, 1 * 60 * 1000)


function inmunePoison(number, cid)
   local n = number
   for i = 1, number do
          addEvent(doRemoveCondition, i*1000, cid, CONDITION_POISON)
       n = n -1
   end
   n = number
   return true
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerFood(cid) >= 120 then
        doPlayerSendCancel(cid, "You are full.")
        doPlayerSendTextMessage(cid,MESSAGE_STATUS_CONSOLE_BLUE, " [Feed]: Full")
        return false
    end
if getPlayerFood(cid) < 120 then
    if getPlayerStorageValue(cid, 44558) == -1 then
doPlayerPopupFYI(cid, "[Tutorial]: Green mushroom!\n\
Use it to gain innmunity to poison condition.\
You can get this with herbalism.\
")
setPlayerStorageValue(cid, 44558, 1)
end
if(hasCondition(cid, CONDITION_EXHAUST)) then
doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
doCreatureSay(cid, "You cant eat so fast.",TALKTYPE_ORANGE_1)
else
local exhausted_seconds = 120
local exhausted_storagevalue = 78959
if (os.time() < getPlayerStorageValue(cid, exhausted_storagevalue)) then
    local tiempo = doNumberFormat(math.ceil((getPlayerStorageValue(cid, exhausted_storagevalue) - os.time())))
    doPlayerSendTextMessage(cid, 19, "[Status]: You already ate green mushroom (remaining time: ".. tiempo .." seconds).")
    return false
end
if (os.time() > getPlayerStorageValue(cid, exhausted_storagevalue)) then
doPlayerSendCancel(cid, "You are feeding.")
    if(not isSorcerer(cid) and not isDruid(cid)) then
    local pos = getPlayerPosition(cid)
        doRemoveCondition(cid, CONDITION_POISON)
        doSendMagicEffect(pos, CONST_ME_MAGIC_BLUE)
        setPlayerStorageValue(cid, exhausted_storagevalue, os.time() + exhausted_seconds)
        doRemoveItem(item.uid,1)
        doPlayerFeed(cid, 14)
        inmunePoison(1*120, cid)
        doSendAnimatedText(pos, "[Innmune] Poison.", 18)
        addEvent(doPlayerSendTextMessage, 1*110*1000, cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
        addEvent(doPlayerSendTextMessage, 1*120*1000, cid, 19, "[Status]: Green mushroom effect has fade.")
        return true
    end

    if(not isKnight(cid) and not isPaladin(cid)) then
        local pos = getPlayerPosition(cid)
        doSendMagicEffect(pos, CONST_ME_MAGIC_BLUE)
        setPlayerStorageValue(cid, exhausted_storagevalue, os.time() + exhausted_seconds)
        doRemoveItem(item.uid,1)
        doPlayerFeed(cid, 14)
        doRemoveCondition(cid, CONDITION_POISON)
        doSendAnimatedText(pos, "[Innmune] Poison.", 18)
        addEvent(doPlayerSendTextMessage, 1*110*1000, cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
        addEvent(doPlayerSendTextMessage, 1*120*1000, cid, 19, "[Status]: Green mushroom effect has fade.")
    end

    return true
end
end
end
end

i've searh on the forums, but the explanations are poor they only say "you must create a local blah and stopEvent(blah)" but that is not enough i need to know how this works properly, a clear example please... try to teach me like you teach your granpa pls... be kind. Thanks
 
Solution
Stop event is only useful if you are using it for something that doesn't require a creatureID.
Such as removing and re-adding a wall after x amount of seconds, where the trigger for this wall addition / removal can be triggered from two separate locations.

In order to stop an event, the event must have a unique marker attached to it.
If the event requires information such as a creatureID, now you're getting into a situation where the unique marker can be changed, or fully removed from the server before the addEvent can trigger, which makes it hard or impossible to determine when the addEvents should stop.

For example, you are using these lines to constantly remove the 'poison icon' from players.
Currently the way you have it scripted...
Stop event is only useful if you are using it for something that doesn't require a creatureID.
Such as removing and re-adding a wall after x amount of seconds, where the trigger for this wall addition / removal can be triggered from two separate locations.

In order to stop an event, the event must have a unique marker attached to it.
If the event requires information such as a creatureID, now you're getting into a situation where the unique marker can be changed, or fully removed from the server before the addEvent can trigger, which makes it hard or impossible to determine when the addEvents should stop.

For example, you are using these lines to constantly remove the 'poison icon' from players.
Currently the way you have it scripted, you are creating 120 addEvents that are using a creatureID 'cid'.
If the player dies, or logs out, your server console is going to be spammed continuously for the remainder of the 2 minutes, because 'cid' can no longer be found.
Creating this many addEvents is also a bad thing to do. Let's think a bit on this.
Imagine 10 people cast this spell.
Now you have 120 * 10 = 1200 scripts running at the same time.
Now while this single spell will likely not lag the server, if you have a bunch of scripts like this, you're gonna crash your server, simply because your computer or the server will run out of memory.
Lua:
inmunePoison(1*120, cid)
Lua:
function inmunePoison(number, cid)
    local n = number
    for i = 1, number do
        addEvent(doRemoveCondition, i*1000, cid, CONDITION_POISON)
        n = n -1
    end
    n = number
    return true
end
When I run into this kind of situation I create a looping self contained function.
The function will continuously run and re-create itself forever until something causes it to stop.
Lua:
function inmunePoison()
    addEvent(inmunePoison, 1000)
    return true
end
Since we are working with a creatureID, I normally create 2 ways for the addEvent to stop itself.
The first way, is if the creature no longer exists. (died or logged out.)
Lua:
function inmunePoison(cid)
    if not isPlayer(cid) then
        return true
    end
    addEvent(inmunePoison, 1000, cid)
    return true
end
The second way, is when it runs out of 'occurrences', which we'll label 'number'.
Lua:
function inmunePoison(cid, number)
    if not isPlayer(cid) then
        return true
    end
    if number > 0 then
        addEvent(inmunePoison, 1000, cid, number - 1)
    end
    return true
end
So at this point the above addEvent will re-create itself every 1000 milliseconds, until 'number' eventually reaches zero, and then the addEvent will stop.

So, now let's add the remove poison like you had before.
Lua:
function inmunePoison(cid, number)
    if not isPlayer(cid) then
        return true
    end
    doRemoveCondition(cid, CONDITION_POISON)
    if number > 0 then
        addEvent(inmunePoison, 1000, cid, number - 1)
    end
    return true
end

And now you simply trigger this function like you would normally.
Lua:
inmunePoison(cid, 120)
If you need the function to happen in the future, use an addEvent.
Lua:
addEvent(inmunePoison, 1000, cid, 120)

Going further into the script, we can add the 'text message' portion to this addEvent as well.
Both variations below will provide the same result. Just showing you a couple ways of displaying your code.
Lua:
function inmunePoison(cid, number)
    if not isPlayer(cid) then
        return true
    end
    doRemoveCondition(cid, CONDITION_POISON)
    if number > 0 then
        if number == 10 then
            doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
        end
        addEvent(inmunePoison, 1000, cid, number - 1)
    else
        doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect has fade.")
    end
    return true
end
Lua:
function inmunePoison(cid, number)
    if not isPlayer(cid) then
        return true
    end
    doRemoveCondition(cid, CONDITION_POISON)
    if number == 10 then
        doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
    elseif number == 0 then
        doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect has fade.")
    end
    if number > 0 then
        addEvent(inmunePoison, 1000, cid, number - 1)
    end
    return true
end

------------------
Going in and fixing the actual script itself, to get get rid of your nested if's. (using if after if after if without using end, when the if statement is already finished, and no longer required.)

First things first..
Since this block is already checking if the food is above or equal to 120, we don't need to check the food again, so we can remove this entire if statement
Also, we are going to change to 'return true', because we want the function to know it completed properly. (even though this isn't the desired end result for the player, it is a desired 'end' for the script.)
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerFood(cid) >= 120 then
        doPlayerSendCancel(cid, "You are full.")
        doPlayerSendTextMessage(cid,MESSAGE_STATUS_CONSOLE_BLUE, " [Feed]: Full")
        return false
    end
    if getPlayerFood(cid) < 120 then
        .
        .
        .
   end
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerFood(cid) >= 120 then
        doPlayerSendCancel(cid, "You are full.")
        doPlayerSendTextMessage(cid,MESSAGE_STATUS_CONSOLE_BLUE, " [Feed]: Full")
        return true
    end
We don't need the excess brackets on this line, so we'll remove those. (and add a space with doCreatureSay, so we keep the script looking clean.)
This is statement is also a 'full stop', so instead of using 'else' we are going to replace it with an end, and stop the script. (so we get rid of another nested if.)
Lua:
if(hasCondition(cid, CONDITION_EXHAUST)) then
        doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
        doCreatureSay(cid, "You cant eat so fast.",TALKTYPE_ORANGE_1)
    else
        .
        .
        .
    end
Lua:
if hasCondition(cid, CONDITION_EXHAUST) then
    doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
    doCreatureSay(cid, "You cant eat so fast.", TALKTYPE_ORANGE_1)
    return true
end
These couple of locals are static, so we are going to move them to the top of the script so they aren't recreated multiple times, and so they are more easily accessible.
Lua:
local exhausted_seconds = 120
local exhausted_storagevalue = 78959
As previously discussed, we'll change this to return true instead of false, so we keep the script coherent.
Lua:
if (os.time() < getPlayerStorageValue(cid, exhausted_storagevalue)) then
        local tiempo = doNumberFormat(math.ceil((getPlayerStorageValue(cid, exhausted_storagevalue) - os.time())))
        doPlayerSendTextMessage(cid, 19, "[Status]: You already ate green mushroom (remaining time: ".. tiempo .." seconds).")
        return false
    end
Lua:
if (os.time() < getPlayerStorageValue(cid, exhausted_storagevalue)) then
        local tiempo = doNumberFormat(math.ceil((getPlayerStorageValue(cid, exhausted_storagevalue) - os.time())))
        doPlayerSendTextMessage(cid, 19, "[Status]: You already ate green mushroom (remaining time: ".. tiempo .." seconds).")
        return true
    end
Once again, this has already been checked previously, so we can simply remove this if statement altogether
Lua:
if (os.time() > getPlayerStorageValue(cid, exhausted_storagevalue)) then
        .
        .
        .
    end
We are going to update this line, to a more proper player text.
Lua:
doPlayerSendCancel(cid, "You are feeding.")
Lua:
doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, "You are feeding.")
This next part is interesting.. because it seems like druids & sorcerers get the 'poison immune buff' however knights and paladins don't.

So let's separate those portions and change the animated text appropriately.
(We'll also remove the check for knights and paladins, because we're going to check if for druids and sorcerers already.)
Lua:
if(not isSorcerer(cid) and not isDruid(cid)) then
        local pos = getPlayerPosition(cid)
        doSendMagicEffect(pos, CONST_ME_MAGIC_BLUE)
        setPlayerStorageValue(cid, exhausted_storagevalue, os.time() + exhausted_seconds)
        doRemoveItem(item.uid,1)
        doPlayerFeed(cid, 14)
        doRemoveCondition(cid, CONDITION_POISON)
        inmunePoison(cid, 120)
        doSendAnimatedText(pos, "[Innmune] Poison.", 18)
        addEvent(doPlayerSendTextMessage, 1*110*1000, cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
        addEvent(doPlayerSendTextMessage, 1*120*1000, cid, 19, "[Status]: Green mushroom effect has fade.")
        return true
    end
 
    if(not isKnight(cid) and not isPaladin(cid)) then
        local pos = getPlayerPosition(cid)
        doSendMagicEffect(pos, CONST_ME_MAGIC_BLUE)
        setPlayerStorageValue(cid, exhausted_storagevalue, os.time() + exhausted_seconds)
        doRemoveItem(item.uid,1)
        doPlayerFeed(cid, 14)
        doRemoveCondition(cid, CONDITION_POISON)
        doSendAnimatedText(pos, "[Innmune] Poison.", 18)
        addEvent(doPlayerSendTextMessage, 1*110*1000, cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
        addEvent(doPlayerSendTextMessage, 1*120*1000, cid, 19, "[Status]: Green mushroom effect has fade.")
    end
Lua:
if isSorcerer(cid) or isDruid(cid) then
        inmunePoison(cid, 120)
        doSendAnimatedText(pos, "[Innmune] Poison.", 18)
    else
        doRemoveCondition(cid, CONDITION_POISON)
        doSendAnimatedText(pos, "Poison Removed.", 18)
    end
Now we're going to move the storage value change underneath the section above, where it checks for that..
And remove the no longer required player text messages, and 'onUse' already provides us with the location of the player, so we'll remove the 'local pos =' line, and update the position information accordingly.
Lua:
setPlayerStorageValue(cid, exhausted_storagevalue, os.time() + exhausted_seconds)
if isSorcerer(cid) or isDruid(cid) then
    inmunePoison(cid, 120)
    doSendAnimatedText(fromPosition, "[Innmune] Poison.", 18)
else
    doRemoveCondition(cid, CONDITION_POISON)
    doSendAnimatedText(fromPosition, "Poison Removed.", 18)
end
doSendMagicEffect(fromPosition, CONST_ME_MAGIC_BLUE)
doRemoveItem(item.uid,1)
doPlayerFeed(cid, 14)
doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, "You are feeding.")
return true

------
and tadaa! our final script.
Lua:
local exhausted_seconds = 120
local exhausted_storagevalue = 78959

local exhaust = createConditionObject(CONDITION_EXHAUST)
setConditionParam(exhaust, CONDITION_PARAM_TICKS, 1 * 60 * 1000)

function inmunePoison(cid, number)
    if not isPlayer(cid) then
        return true
    end
    doRemoveCondition(cid, CONDITION_POISON)
    if number > 0 then
        if number == 10 then
            doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
        end
        addEvent(inmunePoison, 1000, cid, number - 1)
    else
        doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect has fade.")
    end
    return true
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerFood(cid) >= 120 then
        doPlayerSendCancel(cid, "You are full.")
        doPlayerSendTextMessage(cid,MESSAGE_STATUS_CONSOLE_BLUE, " [Feed]: Full")
        return true
    end
    if getPlayerStorageValue(cid, 44558) == -1 then
        doPlayerPopupFYI(cid, "[Tutorial]: Green mushroom!\n\
        Use it to gain innmunity to poison condition.\
        You can get this with herbalism.\
        ")
        setPlayerStorageValue(cid, 44558, 1)
    end
    if hasCondition(cid, CONDITION_EXHAUST) then
        doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
        doCreatureSay(cid, "You cant eat so fast.", TALKTYPE_ORANGE_1)
        return true
    end
    if (os.time() < getPlayerStorageValue(cid, exhausted_storagevalue)) then
        local tiempo = doNumberFormat(math.ceil((getPlayerStorageValue(cid, exhausted_storagevalue) - os.time())))
        doPlayerSendTextMessage(cid, 19, "[Status]: You already ate green mushroom (remaining time: ".. tiempo .." seconds).")
        return true
    end
    setPlayerStorageValue(cid, exhausted_storagevalue, os.time() + exhausted_seconds)
    if isSorcerer(cid) or isDruid(cid) then
        inmunePoison(cid, 120)
        doSendAnimatedText(fromPosition, "[Innmune] Poison.", 18)
    else
        doRemoveCondition(cid, CONDITION_POISON)
        doSendAnimatedText(fromPosition, "Poison Removed.", 18)
    end
    doSendMagicEffect(fromPosition, CONST_ME_MAGIC_BLUE)
    doRemoveItem(item.uid, 1)
    doPlayerFeed(cid, 14)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, "You are feeding.")
    return true
end
 
Last edited:
Solution
Stop event is only useful if you are using it for something that doesn't require a creatureID.
Such as removing and re-adding a wall after x amount of seconds, where the trigger for this wall addition / removal can be triggered from two separate locations.

In order to stop an event, the event must have a unique marker attached to it.
If the event requires information such as a creatureID, now you're getting into a situation where the unique marker can be changed, or fully removed from the server before the addEvent can trigger, which makes it hard or impossible to determine when the addEvents should stop.

For example, you are using these lines to constantly remove the 'poison icon' from players.
Currently the way you have it scripted, you are creating 120 addEvents that are using a creatureID 'cid'.
If the player dies, or logs out, your server console is going to be spammed continuously for the remainder of the 2 minutes, because 'cid' can no longer be found.
Creating this many addEvents is also a bad thing to do. Let's think a bit on this.
Imagine 10 people cast this spell.
Now you have 120 * 10 = 1200 scripts running at the same time.
Now while this single spell will likely not lag the server, if you have a bunch of scripts like this, you're gonna crash your server, simply because your computer or the server will run out of memory.
Lua:
inmunePoison(1*120, cid)
Lua:
function inmunePoison(number, cid)
    local n = number
    for i = 1, number do
        addEvent(doRemoveCondition, i*1000, cid, CONDITION_POISON)
        n = n -1
    end
    n = number
    return true
end
When I run into this kind of situation I create a looping self contained function.
The function will continuously run and re-create itself forever until something causes it to stop.
Lua:
function inmunePoison()
    addEvent(inmunePoison, 1000)
    return true
end
Since we are working with a creatureID, I normally create 2 ways for the addEvent to stop itself.
The first way, is if the creature no longer exists. (died or logged out.)
Lua:
function inmunePoison(cid)
    if not isPlayer(cid) then
        return true
    end
    addEvent(inmunePoison, 1000, cid)
    return true
end
The second way, is when it runs out of 'occurrences', which we'll label 'number'.
Lua:
function inmunePoison(cid, number)
    if not isPlayer(cid) then
        return true
    end
    if number > 0 then
        addEvent(inmunePoison, 1000, cid, number - 1)
    end
    return true
end
So at this point the above addEvent will re-create itself every 1000 milliseconds, until 'number' eventually reaches zero, and then the addEvent will stop.

So, now let's add the remove poison like you had before.
Lua:
function inmunePoison(cid, number)
    if not isPlayer(cid) then
        return true
    end
    doRemoveCondition(cid, CONDITION_POISON)
    if number > 0 then
        addEvent(inmunePoison, 1000, cid, number - 1)
    end
    return true
end

And now you simply trigger this function like you would normally.
Lua:
inmunePoison(cid, 120)
If you need the function to happen in the future, use an addEvent.
Lua:
addEvent(inmunePoison, 1000, cid, 120)

Going further into the script, we can add the 'text message' portion to this addEvent as well.
Both variations below will provide the same result. Just showing you a couple ways of displaying your code.
Lua:
function inmunePoison(cid, number)
    if not isPlayer(cid) then
        return true
    end
    doRemoveCondition(cid, CONDITION_POISON)
    if number > 0 then
        if number == 10 then
            doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
        end
        addEvent(inmunePoison, 1000, cid, number - 1)
    else
        doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect has fade.")
    end
    return true
end
Lua:
function inmunePoison(cid, number)
    if not isPlayer(cid) then
        return true
    end
    doRemoveCondition(cid, CONDITION_POISON)
    if number == 10 then
        doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
    elseif number == 0 then
        doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect has fade.")
    end
    if number > 0 then
        addEvent(inmunePoison, 1000, cid, number - 1)
    end
    return true
end

------------------
Going in and fixing the actual script itself, to get get rid of your nested if's. (using if after if after if without using end, when the if statement is already finished, and no longer required.)

First things first..
Since this block is already checking if the food is above or equal to 120, we don't need to check the food again, so we can remove this entire if statement
Also, we are going to change to 'return true', because we want the function to know it completed properly. (even though this isn't the desired end result for the player, it is a desired 'end' for the script.)
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerFood(cid) >= 120 then
        doPlayerSendCancel(cid, "You are full.")
        doPlayerSendTextMessage(cid,MESSAGE_STATUS_CONSOLE_BLUE, " [Feed]: Full")
        return false
    end
    if getPlayerFood(cid) < 120 then
        .
        .
        .
   end
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerFood(cid) >= 120 then
        doPlayerSendCancel(cid, "You are full.")
        doPlayerSendTextMessage(cid,MESSAGE_STATUS_CONSOLE_BLUE, " [Feed]: Full")
        return true
    end
We don't need the excess brackets on this line, so we'll remove those. (and add a space with doCreatureSay, so we keep the script looking clean.)
This is statement is also a 'full stop', so instead of using 'else' we are going to replace it with an end, and stop the script. (so we get rid of another nested if.)
Lua:
if(hasCondition(cid, CONDITION_EXHAUST)) then
        doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
        doCreatureSay(cid, "You cant eat so fast.",TALKTYPE_ORANGE_1)
    else
        .
        .
        .
    end
Lua:
if hasCondition(cid, CONDITION_EXHAUST) then
    doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
    doCreatureSay(cid, "You cant eat so fast.", TALKTYPE_ORANGE_1)
    return true
end
These couple of locals are static, so we are going to move them to the top of the script so they aren't recreated multiple times, and so they are more easily accessible.
Lua:
local exhausted_seconds = 120
local exhausted_storagevalue = 78959
As previously discussed, we'll change this to return true instead of false, so we keep the script coherent.
Lua:
if (os.time() < getPlayerStorageValue(cid, exhausted_storagevalue)) then
        local tiempo = doNumberFormat(math.ceil((getPlayerStorageValue(cid, exhausted_storagevalue) - os.time())))
        doPlayerSendTextMessage(cid, 19, "[Status]: You already ate green mushroom (remaining time: ".. tiempo .." seconds).")
        return false
    end
Lua:
if (os.time() < getPlayerStorageValue(cid, exhausted_storagevalue)) then
        local tiempo = doNumberFormat(math.ceil((getPlayerStorageValue(cid, exhausted_storagevalue) - os.time())))
        doPlayerSendTextMessage(cid, 19, "[Status]: You already ate green mushroom (remaining time: ".. tiempo .." seconds).")
        return true
    end
Once again, this has already been checked previously, so we can simply remove this if statement altogether
Lua:
if (os.time() > getPlayerStorageValue(cid, exhausted_storagevalue)) then
        .
        .
        .
    end
We are going to update this line, to a more proper player text.
Lua:
doPlayerSendCancel(cid, "You are feeding.")
Lua:
doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, "You are feeding.")
This next part is interesting.. because it seems like druids & sorcerers get the 'poison immune buff' however knights and paladins don't.

So let's separate those portions and change the animated text appropriately.
(We'll also remove the check for knights and paladins, because we're going to check if for druids and sorcerers already.)
Lua:
if(not isSorcerer(cid) and not isDruid(cid)) then
        local pos = getPlayerPosition(cid)
        doSendMagicEffect(pos, CONST_ME_MAGIC_BLUE)
        setPlayerStorageValue(cid, exhausted_storagevalue, os.time() + exhausted_seconds)
        doRemoveItem(item.uid,1)
        doPlayerFeed(cid, 14)
        doRemoveCondition(cid, CONDITION_POISON)
        inmunePoison(cid, 120)
        doSendAnimatedText(pos, "[Innmune] Poison.", 18)
        addEvent(doPlayerSendTextMessage, 1*110*1000, cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
        addEvent(doPlayerSendTextMessage, 1*120*1000, cid, 19, "[Status]: Green mushroom effect has fade.")
        return true
    end
 
    if(not isKnight(cid) and not isPaladin(cid)) then
        local pos = getPlayerPosition(cid)
        doSendMagicEffect(pos, CONST_ME_MAGIC_BLUE)
        setPlayerStorageValue(cid, exhausted_storagevalue, os.time() + exhausted_seconds)
        doRemoveItem(item.uid,1)
        doPlayerFeed(cid, 14)
        doRemoveCondition(cid, CONDITION_POISON)
        doSendAnimatedText(pos, "[Innmune] Poison.", 18)
        addEvent(doPlayerSendTextMessage, 1*110*1000, cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
        addEvent(doPlayerSendTextMessage, 1*120*1000, cid, 19, "[Status]: Green mushroom effect has fade.")
    end
Lua:
if isSorcerer(cid) or isDruid(cid) then
        inmunePoison(cid, 120)
        doSendAnimatedText(pos, "[Innmune] Poison.", 18)
    else
        doRemoveCondition(cid, CONDITION_POISON)
        doSendAnimatedText(pos, "Poison Removed.", 18)
    end
Now we're going to move the storage value change underneath the section above, where it checks for that..
And remove the no longer required player text messages, and 'onUse' already provides us with the location of the player, so we'll remove the 'local pos =' line, and update the position information accordingly.
Lua:
setPlayerStorageValue(cid, exhausted_storagevalue, os.time() + exhausted_seconds)
if isSorcerer(cid) or isDruid(cid) then
    inmunePoison(cid, 120)
    doSendAnimatedText(fromPosition, "[Innmune] Poison.", 18)
else
    doRemoveCondition(cid, CONDITION_POISON)
    doSendAnimatedText(fromPosition, "Poison Removed.", 18)
end
doSendMagicEffect(fromPosition, CONST_ME_MAGIC_BLUE)
doRemoveItem(item.uid,1)
doPlayerFeed(cid, 14)
doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, "You are feeding.")
return true

------
and tadaa! our final script.
Lua:
local exhausted_seconds = 120
local exhausted_storagevalue = 78959

local exhaust = createConditionObject(CONDITION_EXHAUST)
setConditionParam(exhaust, CONDITION_PARAM_TICKS, 1 * 60 * 1000)

function inmunePoison(cid, number)
    if not isPlayer(cid) then
        return true
    end
    doRemoveCondition(cid, CONDITION_POISON)
    if number > 0 then
        if number == 10 then
            doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect will fade in 10 seconds.")
        end
        addEvent(inmunePoison, 1000, cid, number - 1)
    else
        doPlayerSendTextMessage(cid, 19, "[Status]: Green mushroom effect has fade.")
    end
    return true
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerFood(cid) >= 120 then
        doPlayerSendCancel(cid, "You are full.")
        doPlayerSendTextMessage(cid,MESSAGE_STATUS_CONSOLE_BLUE, " [Feed]: Full")
        return true
    end
    if getPlayerStorageValue(cid, 44558) == -1 then
        doPlayerPopupFYI(cid, "[Tutorial]: Green mushroom!\n\
        Use it to gain innmunity to poison condition.\
        You can get this with herbalism.\
        ")
        setPlayerStorageValue(cid, 44558, 1)
    end
    if hasCondition(cid, CONDITION_EXHAUST) then
        doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
        doCreatureSay(cid, "You cant eat so fast.", TALKTYPE_ORANGE_1)
        return true
    end
    if (os.time() < getPlayerStorageValue(cid, exhausted_storagevalue)) then
        local tiempo = doNumberFormat(math.ceil((getPlayerStorageValue(cid, exhausted_storagevalue) - os.time())))
        doPlayerSendTextMessage(cid, 19, "[Status]: You already ate green mushroom (remaining time: ".. tiempo .." seconds).")
        return true
    end
    setPlayerStorageValue(cid, exhausted_storagevalue, os.time() + exhausted_seconds)
    if isSorcerer(cid) or isDruid(cid) then
        inmunePoison(cid, 120)
        doSendAnimatedText(fromPosition, "[Innmune] Poison.", 18)
    else
        doRemoveCondition(cid, CONDITION_POISON)
        doSendAnimatedText(fromPosition, "Poison Removed.", 18)
    end
    doSendMagicEffect(fromPosition, CONST_ME_MAGIC_BLUE)
    doRemoveItem(item.uid, 1)
    doPlayerFeed(cid, 14)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, "You are feeding.")
    return true
end


oh boi, i feel like garbage, this reply should be part of the basics tutorial, may i quote this for those developers who are like me? a total failure


congratulations you enlightenment me now i understand learn something REALLY usefull. this is the way to teach newbies. Clap
 
Back
Top