• 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 Starving script, Possible?

popoverde

Intermediate OT User
Joined
Jan 16, 2015
Messages
202
Reaction score
135
Location
[RPG]
The Forgotten Server - Version 0.3 (Crying Damson).


Example: If a player dont eat in X time he start losing HP .




Thanks.
 
Code:
function onThink(interval, lastExecution)
     for _, cid in pairs(getPlayersOnline()) do
         if getPlayerFood(cid) == 0 then
             doCreatureAddHealth(cid, -10)
             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You are starving.")
         end
     end
     return true
end
 
Code:
function onThink(interval, lastExecution)
     for _, cid in pairs(getPlayersOnline()) do
         if getPlayerFood(cid) == 0 then
             doCreatureAddHealth(cid, -10)
             doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You are starving.")
         end
     end
     return true
end

Not really performance friendly. Probably the best way would be to add starving condition when regeneration condition ends in sources or editing food script to add starving (addEvent) after player feed time, also checking it onLogin.
 
I made a new file called starving.lua then put all that inside

went to globalevents.xml and wrote


Code:
<?xml version="1.0" encoding="UTF-8"?>
<globalevents>
<globalevent name="save" interval="900" script="save.lua"/>
<globalevent name="starving" interval="900" script="starving.lua"/>
<globalevent name="clean" interval="7200" script="clean.lua"/>
</globalevents>

And i think is not working. my char is stucked at 57hp not regenerating that means he has no food right?

and hes not losing any hp point

ERROR:



[27/01/2015 18:58:42] Lua Script Error: [GlobalEvent Interface]
[27/01/2015 18:58:42] data/globalevents/scripts/starving.lua:eek:nThink

[27/01/2015 18:58:42] data/globalevents/scripts/starving.lua:2: attempt to call global 'getPlayersOnline' (a nil value)
[27/01/2015 18:58:42] stack traceback:
[27/01/2015 18:58:42] data/globalevents/scripts/starving.lua:2: in function <data/globalevents/scripts/starving.lua:1>
[27/01/2015 18:58:42] [Error - GlobalEvents::eek:nThink] Couldn't execute event: starving
 
Last edited:
Not really performance friendly. Probably the best way would be to add starving condition when regeneration condition ends in sources or editing food script to add starving (addEvent) after player feed time, also checking it onLogin.

If you know a better way.. go for it and post here hehe
 
Thanks but still dont work, got another error:


Code:
[27/01/2015  20:19:53] Lua Script Error: [GlobalEvent Interface]
[27/01/2015  20:19:53] data/globalevents/scripts/starving.lua:onThink

[27/01/2015  20:19:53] data/globalevents/scripts/starving.lua:2: attempt to call global 'getPlayersOnlineList' (a nil value)
[27/01/2015  20:19:53] stack traceback:
[27/01/2015  20:19:53]     data/globalevents/scripts/starving.lua:2: in function <data/globalevents/scripts/starving.lua:1>
[27/01/2015  20:19:53] [Error - GlobalEvents::onThink] Couldn't execute event: starving
 
Im 100% sure yes,

[27/01/2015 20:45:48] The Forgotten Server - Version 0.3 (Crying Damson).

The client im running is 7.6

Sorry but What you mean by sources?
 
Not really performance friendly. Probably the best way would be to add starving condition when regeneration condition ends in sources or editing food script to add starving (addEvent) after player feed time, also checking it onLogin.
That is indeed a better idea.


function.lua
Code:
function doPlayerStarve(cid)
     if isPlayer(cid) == FALSE or getPlayerFood(cid) > 0 then
         return TRUE
     end
     doCreatureAddHealth(cid, -10)
     doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You are starving.")
     addEvent(doPlayerStarve, 60 * 1000, cid)
     return TRUE
end

login.lua
Code:
addEvent(doPlayerStarve, 60 * 1000, cid)

food.lua (above the last return true)
Code:
addEvent(doPlayerStarve, (getPlayerFood(cid) + 60) * 1000, cid)
 
Last edited:
WORKS Thank you all!!!!

but, im getting the next error in console:




[27/01/2015 21:15:00] Lua Script Error: [CreatureScript Interface]
[27/01/2015 21:15:00] in a timer event called from:
[27/01/2015 21:15:00] data/creaturescripts/scripts/login.lua:eek:nLogin

[27/01/2015 21:15:00] luaDoCreatureAddHealth(). Creature not found

[27/01/2015 21:15:00] Lua Script Error: [CreatureScript Interface]
[27/01/2015 21:15:00] in a timer event called from:
[27/01/2015 21:15:00] data/creaturescripts/scripts/login.lua:eek:nLogin

[27/01/2015 21:15:00] luaDoPlayerSendTextMessage(). Player not found


My login.lua:

Code:
function onLogin(cid)
    registerCreatureEvent(cid, "PlayerDeath")
    addEvent(doPlayerStarve, 60 * 1000, cid)
        return TRUE

end
 
Did you use the first version I posted or the one I have now after editing my post?
The first version was without FALSE and TRUE (for newer servers).
 
Im using the last one, removed the previous from globalevents:

my function.lua: (Not all obviously, just first lines)


Code:
function doPlayerStarve(cid)
     if not isPlayer(cid) or getPlayerFood(cid) > 0 then
         return true
     end
     doCreatureAddHealth(cid, -10)
     doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You are starving.")
     addEvent(doPlayerStarve, 60 * 1000, cid)
     return true
end

function doPlayerGiveItem(cid, itemid, count, charges)
    local hasCharges = (isItemRune(itemid) == TRUE or isItemFluidContainer(itemid) == TRUE)
    if(hasCharges and charges == nil) then
        charges = 1
    end
    while count > 0 do
        local tempcount = 1
        if(hasCharges) then
            tempcount = charges
        end

    end


my food.lua:


Code:
local MAX_FOOD = 1200

function onUse(cid, item, frompos, item2, topos)
    if (item.itemid == 6280) then
        doPlayerSay(cid, "Blewing out the candle.", 1)
        doTransformItem(item.uid, item.itemid - 1)
        doSendMagicEffect(frompos, CONST_ME_POFF)
        return TRUE
    end

    if FOODS[item.itemid] == nil then
        return FALSE
    end

    local size = FOODS[item.itemid][1]
    local sound = FOODS[item.itemid][2]

    if (getPlayerFood(cid) + size > MAX_FOOD) then
        doPlayerSendCancel(cid, "You are full.")
        return TRUE
    end
    doPlayerFeed(cid, size)
    doRemoveItem(item.uid, 1)
    doPlayerSay(cid, sound, 1)
    doCreatureSay(cid, sound, TALKTYPE_MONSTER_SAY)
        addEvent(doPlayerStarve, (getPlayerFood(cid) + 60) * 1000, cid)
    return TRUE


end


my login.lua:



Code:
function onLogin(cid)
    registerCreatureEvent(cid, "PlayerDeath")
    addEvent(doPlayerStarve, 60 * 1000, cid)
        return TRUE

end
 
I mean this, I edited my post 6 minutes after that when I saw you used an old server.
function.lua
Code:
function doPlayerStarve(cid)
     if isPlayer(cid) == FALSE or getPlayerFood(cid) > 0 then
         return TRUE
     end
     doCreatureAddHealth(cid, -10)
     doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You are starving.")
     addEvent(doPlayerStarve, 60 * 1000, cid)
     return TRUE
end
 
Back
Top