• 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 Life regen out of battle

Zazeros

Member
Joined
Feb 13, 2012
Messages
67
Reaction score
17
0.4
Morning guys, I was trying to do this on my own, but got no luck
I think its simple, but I fear, the way I was thinking, will cause lag if a lot of players are online.

I want the script to this: When the player is out of battle (without the x), he will start to regen health, like 5% each 5 sec and will be cancelled if he enters battle.
It will cause lag?
 
Solution
0.4
Morning guys, I was trying to do this on my own, but got no luck
I think its simple, but I fear, the way I was thinking, will cause lag if a lot of players are online.

I want the script to this: When the player is out of battle (without the x), he will start to regen health, like 5% each 5 sec and will be cancelled if he enters battle.
It will cause lag?
Won't cause lag.

and, here you go.
(note: don't use /reload, or it'll break the addEvent.)
XML:
<event type="login" name="playerRegeneration" event="script" value="playerRegeneration.lua"/>
Lua:
local function regenPercentageHealthOverTime(cid, percentage, delay)
    if not isPlayer(cid) then
        return true
    end
    if getCreatureCondition(cid, CONDITION_INFIGHT)...
0.4
Morning guys, I was trying to do this on my own, but got no luck
I think its simple, but I fear, the way I was thinking, will cause lag if a lot of players are online.

I want the script to this: When the player is out of battle (without the x), he will start to regen health, like 5% each 5 sec and will be cancelled if he enters battle.
It will cause lag?
Won't cause lag.

and, here you go.
(note: don't use /reload, or it'll break the addEvent.)
XML:
<event type="login" name="playerRegeneration" event="script" value="playerRegeneration.lua"/>
Lua:
local function regenPercentageHealthOverTime(cid, percentage, delay)
    if not isPlayer(cid) then
        return true
    end
    if getCreatureCondition(cid, CONDITION_INFIGHT) == false then
        local currentHealth = getCreatureHealth(cid)
        local maxHealth = getCreatureMaxHealth(cid)
        if currentHealth < maxHealth then
            local percentHealth = (maxHealth / 100) * percentage
            if currentHealth + percentHealth > maxHealth then
                percentHealth = maxHealth - currentHealth
            end
            doCreatureAddHealth(cid, percentHealth)
        end
    end
    addEvent(regenPercentageHealthOverTime, delay, cid, percentage, delay)
end

function onLogin(cid)
    regenPercentageHealthOverTime(cid, 5, 5000)
    return true
end
 
Solution
Won't cause lag.

and, here you go.
(note: don't use /reload, or it'll break the addEvent.)
XML:
<event type="login" name="playerRegeneration" event="script" value="playerRegeneration.lua"/>
Lua:
local function regenPercentageHealthOverTime(cid, percentage, delay)
    if not isPlayer(cid) then
        return true
    end
    if getCreatureCondition(cid, CONDITION_INFIGHT) == false then
        local currentHealth = getCreatureHealth(cid)
        local maxHealth = getCreatureMaxHealth(cid)
        if currentHealth < maxHealth then
            local percentHealth = (maxHealth / 100) * percentage
            if currentHealth + percentHealth > maxHealth then
                percentHealth = maxHealth - currentHealth
            end
            doCreatureAddHealth(cid, percentHealth)
        end
    end
    addEvent(regenPercentageHealthOverTime(cid), delay, cid, percentage, delay)
end

function onLogin(cid)
    regenPercentageHealthOverTime(cid, 5, 5000)
    return true
end
In addEvent the first argument should just be the function name without parameters, so addEvent(regenPercentageHealthOverTime, delay, cid, percentage, delay)
 
In addEvent the first argument should just be the function name without parameters, so addEvent(regenPercentageHealthOverTime, delay, cid, percentage, delay)
good catch.
 
@Xikini It worked, kinda
When the player log in, and don't have battle, it will regen his life. But, if he enters a battle then gets off of battle, it will not regen. He has to log out and then log in for to work again
 
@Xikini It worked, kinda
When the player log in, and don't have battle, it will regen his life. But, if he enters a battle then gets off of battle, it will not regen. He has to log out and then log in for to work again
Here's the same script, but with prints to the console, to help with testing.

Let us know what prints to the console when it stops working.
Lua:
local function regenPercentageHealthOverTime(cid, percentage, delay)
    print("-------")
    if not isPlayer(cid) then
        print("regen stopped. Player no longer exists.")
        return true
    end
    print(getCreatureName(cid))
    if getCreatureCondition(cid, CONDITION_INFIGHT) == false then
        print("Regen is active.")
        local currentHealth = getCreatureHealth(cid)
        local maxHealth = getCreatureMaxHealth(cid)
        if currentHealth < maxHealth then
            local percentHealth = (maxHealth / 100) * percentage
            if currentHealth + percentHealth > maxHealth then
                percentHealth = maxHealth - currentHealth
            end
            doCreatureAddHealth(cid, percentHealth)
            print("Player recovered " .. percentHealth .. " health.")
        else
            print("Player is at max health. No healing provided.")
        end
    else
        print("Regen is in-active due to combat.")
    end
    addEvent(regenPercentageHealthOverTime, delay, cid, percentage, delay)
end

function onLogin(cid)
    regenPercentageHealthOverTime(cid, 5, 5000)
    return true
end
 
Here's the same script, but with prints to the console, to help with testing.

Let us know what prints to the console when it stops working.
Lua:
local function regenPercentageHealthOverTime(cid, percentage, delay)
    print("-------")
    if not isPlayer(cid) then
        print("regen stopped. Player no longer exists.")
        return true
    end
    print(getCreatureName(cid))
    if getCreatureCondition(cid, CONDITION_INFIGHT) == false then
        print("Regen is active.")
        local currentHealth = getCreatureHealth(cid)
        local maxHealth = getCreatureMaxHealth(cid)
        if currentHealth < maxHealth then
            local percentHealth = (maxHealth / 100) * percentage
            if currentHealth + percentHealth > maxHealth then
                percentHealth = maxHealth - currentHealth
            end
            doCreatureAddHealth(cid, percentHealth)
            print("Player recovered " .. percentHealth .. " health.")
        else
            print("Player is at max health. No healing provided.")
        end
    else
        print("Regen is in-active due to combat.")
    end
    addEvent(regenPercentageHealthOverTime, delay, cid, percentage, delay)
end

function onLogin(cid)
    regenPercentageHealthOverTime(cid, 5, 5000)
    return true
end
Logging out, then logging in again before the 5 seconds to check if player exist has passed, will cause the event to be duplicated.
 
Logging out, then logging in again before the 5 seconds to check if player exist has passed, will cause the event to be duplicated.
You are incorrect.
The cid information in each addEvent would be different, therefore is not a duplicate.
 
Back
Top