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

TFS 0.X Save pet health for 2 minutes

Lurk

Active Member
Joined
Dec 4, 2017
Messages
336
Reaction score
48
Hello, I have a pet system and I have this problem:

You summon your pet and it spawns full life, as intented. You can also at any time unspawn it by using the item again;
If my pet loses let's say 80% of it's health, I can simply use my item again to unspawn and then use again to spawn it full health

I want to check if the pet is at full health when despawning and, if it's not, save it's current health for 2 minutes so if someone tries to spawn the pet again it'll spawn with the amount of health it had before.

simple_pet.lua
Lua:
function onUse(cid, item, frompos, item2, topos)
local dolls = {
[11256] = {pet = "Crystal Spider"},
[11207] = {pet = "Ashmunrah"},
[11144] = {pet = "Demon"},
[9019] = {pet = "Vampire"},
}

local go = dolls[item.itemid]
local summon = getCreatureSummons(cid)
---------------------------------------------------
if #summon >= 1 then
for _, pid in ipairs(summon) do
doRemoveCreature(pid)
doCreatureSay(cid, "Can go rest ["..go.pet.."]", TALKTYPE_ORANGE_1)
end
return true
end

doConvinceCreature(cid, doSummonCreature(go.pet, getCreaturePosition(cid)))
doCreatureSay(cid, "Let battle ["..go.pet.."]", TALKTYPE_ORANGE_1)
return true
end

actions.xml
XML:
<action itemid="11256;11207;11144;9019" event="script" value="simple_pet.lua"/>
 
Solution
So first things first, unless something in your table requires you to reload it constantly, put your config table outside of the function.

And the rest of it can simply be put into a global table.
(Note: I'm assuming that you only have 1 of each type of pet, and that you can only have 1 pet out at a time.)
Lua:
local dolls = {
    [11256] = {pet = "Crystal Spider"},
    [11207] = {pet = "Ashmunrah"},
    [11144] = {pet = "Demon"},
    [9019] = {pet = "Vampire"},
}
local previous_summons = {}
local hp_regen_timer = 120 -- in seconds, if summoning the same monster before this timer elaspes, then summon at previously known hp.

function onUse(cid, item, frompos, item2, topos)

    local go = dolls[item.itemid]
    local summon = getCreatureSummons(cid)...
So first things first, unless something in your table requires you to reload it constantly, put your config table outside of the function.

And the rest of it can simply be put into a global table.
(Note: I'm assuming that you only have 1 of each type of pet, and that you can only have 1 pet out at a time.)
Lua:
local dolls = {
    [11256] = {pet = "Crystal Spider"},
    [11207] = {pet = "Ashmunrah"},
    [11144] = {pet = "Demon"},
    [9019] = {pet = "Vampire"},
}
local previous_summons = {}
local hp_regen_timer = 120 -- in seconds, if summoning the same monster before this timer elaspes, then summon at previously known hp.

function onUse(cid, item, frompos, item2, topos)

    local go = dolls[item.itemid]
    local summon = getCreatureSummons(cid)
    local cur_time = os.time()
    if not previous_summons[cid] then previous_summons[cid] = {} end
    ---------------------------------------------------
    if #summon >= 1 then
        for _, pid in ipairs(summon) do
            previous_summons[cid][item.itemid] = {cur_time + hp_regen_timer, getCreatureHealth(pid)}
            doRemoveCreature(pid)
            doCreatureSay(cid, "Can go rest ["..go.pet.."]", TALKTYPE_ORANGE_1)
        end
        return true
    end
   
    local summon = doSummonCreature(go.pet, getCreaturePosition(cid))
    doConvinceCreature(cid, summon)
    if previous_summons[cid][item.itemid] and previous_summons[cid][item.itemid][1] > cur_time then
        local remove_hp = -(getCreatureMaxHealth(summon) - previous_summons[cid][item.itemid][2])
        doCreatureAddHealth(summon, remove_hp)
    end
    doCreatureSay(cid, "Let battle ["..go.pet.."]", TALKTYPE_ORANGE_1)
    return true
end
 
Last edited:
Solution
Back
Top