• 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 Error on regeneration tile

ralke

(҂ ͠❛ ෴ ͡❛)ᕤ
Joined
Dec 17, 2011
Messages
1,521
Solutions
27
Reaction score
870
Location
Santiago - Chile
GitHub
ralke23
Twitch
ralke23
Hi, I had this script in a previous version based on Tfs 1.3 and worked fine, but now console show this error on Tfs 1.3 revscript. Im using 8.6 version. someone would help me fixing this script? Thanks in advance!

regeneration tile.pngregeneration tile.png
regen.lua

Lua:
local dirs = {
    [0] = {x = 0,  y = -1, nx = 7},  -- North
    [1] = {x = 1,  y = 0,  nx = 5},   -- East
    [2] = {x = 0,  y = 1,  nx = 4},   -- South
    [3] = {x = -1, y = 0,  nx = 6},  -- West
    [4] = {x = -1, y = 1,  nx = 3},  -- Southwest
    [5] = {x = 1,  y = 1,  nx = 2},   -- Southeast
    [6] = {x = -1, y = -1, nx = 0}, -- Northwest
    [7] = {x = 1,  y = -1, nx = 1}   -- Northeast
}

local function executeShit(name, areaEffect, distEffect, percent, delay, currDir, totalHealth, n)
    -- currDir, totalHealth, n args are for internal usage
    local player = Player(name)
    if not player then
        return
    end
    if (n == nil or n <= 7) then
        local pos = player:getPosition()
        local off = dirs[currDir or 0]
        local position = Position(pos.x+off.x, pos.y+off.y, pos.z)
        local health = player:addPercentHealth(percent)
        local add = totalHealth and totalHealth + health or health
        position:sendMagicEffect(areaEffect)
        position:sendDistanceEffect(pos, distEffect)
        pos:sendMagicEffect(CONST_ME_HOLYAREA)
        player:sendTextMessage(MESSAGE_STATUS_SMALL, string.format('Total health added: %d', add))
        addEvent(executeShit, delay, name, areaEffect, distEffect, percent, delay, off.nx, add, not n and 1 or n+1)
    end
end

-------------------------------------------------------------------------------------------------------------------

local wait = {}

local cfg = {
    percent = 20, -- percent of health to be added back to the player each tick
    time = 30, -- seconds to use tile again
    delay = 650, -- milliseconds (tick delay)
    areaEffect = CONST_ME_HOLYDAMAGE,
    distanceEffect = CONST_ANI_HOLY
}

function onStepIn(creature, item, position, fromPosition)
    local p = creature:getPlayer()
    if p then
        local getwait = wait[p:getName()]
        if getwait and getwait - os.time() > 0 then
            return p:sendTextMessage(MESSAGE_STATUS_SMALL, string.format('You must wait %d seconds to use this again.', getwait - os.time())), creature:teleportTo(fromPosition, true)
        end
        executeShit(p:getName(), cfg.areaEffect, cfg.distanceEffect, cfg.percent/100, cfg.delay)
        wait[p:getName()] = os.time() + cfg.time
        return
    end
    return creature:teleportTo(fromPosition, true)
end
 
Last edited:
Solution
"attempt to call method 'addPercentHealth' (a nil value)" means it does not exist
addPercentHealth is not a default method so you will have to create it

in lib/core/creature.lua add
Lua:
function Creature.addPercentHealth(self, percent)
    return self:addHealth(self:getMaxHealth() * (percent / 100))
end

I recommend to read this to gain understanding of most errors in lua
"attempt to call method 'addPercentHealth' (a nil value)" means it does not exist
addPercentHealth is not a default method so you will have to create it

in lib/core/creature.lua add
Lua:
function Creature.addPercentHealth(self, percent)
    return self:addHealth(self:getMaxHealth() * (percent / 100))
end

I recommend to read this to gain understanding of most errors in lua
 
Last edited:
Solution
"attempt to call method 'addPercentHealth' (a nil value)" means it does not exist
addPercentHealth is not a default method so you will have to create it
in lib/core/player.lua add
Lua:
function Player.addPercentHealth(self, percent)
    return Creature.addPercentHealth(self, percent)
end
in lib/core/creature.lua add
Lua:
function Creature.addPercentHealth(self, percent)
    return self:addHealth(self:getMaxHealth() * (percent / 100))
end

I recommend to read this to gain understanding of most errors in lua
Note that you don't need to add that player function, only the creature one and the player class will inherit it automatically.
 
Note that you don't need to add that player function, only the creature one and the player class will inherit it automatically.
Thanks for clarifying, I was unsure about that and didn't have time to look it up.

Updated my previous post
 
"attempt to call method 'addPercentHealth' (a nil value)" means it does not exist
addPercentHealth is not a default method so you will have to create it

in lib/core/creature.lua add
Lua:
function Creature.addPercentHealth(self, percent)
    return self:addHealth(self:getMaxHealth() * (percent / 100))
end

I recommend to read this to gain understanding of most errors in lua

thanks a lot for responding! but sadly is not working, im having this error on test. I tried too with adding player function and didn't work either
Anyway, the link you attached is very useful ^^

error_regen.png

edit------
I would also like to use this topic to solve this error in my quest system (specifically in system.lua) where doCopyItem is not recognized. I attached the system.lua in case someone needs it to fix the error
quest_system.png

thanks in advance!
 

Attachments

Last edited:
This line is expecting player:addPercentHealth(percent) to return a number value to store
Lua:
local health = player:addPercentHealth(percent)
But it returns a boolean to let us know if health got added or not.
To know how much health that is, we should create a new function (in accordance to curly's law)
again in lib/core/creature.lua add
Lua:
function Creature.getPercentHealth(self, percent)
    return self:getMaxHealth() * (percent / 100)
end
then change
Lua:
local health = player:addPercentHealth(percent)
to
Lua:
local health = player:getPercentHealth(percent)
player:addPercentHealth(percent)
 
@Merlin thanks a lot!! added this function too and now is working excelent! i forgot where i took the script but the original thread helped me fixing. thread solved!

Lua:
function Player:getMissingHealth()
    return self:getMaxHealth() - self:getHealth()
end

function Player:addPercentHealth(value)
    local health = math.floor(self:getMissingHealth() * value+0.5)
    local final = (health > 0 and health) or 1
    if value <= 1.0 and self:getHealth() ~= self:getMaxHealth() then
        self:addHealth(final)
        return final
    end
    return 0
end

in case someone wants to help... any idea about the system.lua error?
 

Attachments

Back
Top