• 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 Help with Syntax problem in spell "player nil value"

Icaraii

Well-Known Member
Joined
Jan 5, 2020
Messages
469
Solutions
1
Reaction score
58
Hi,

I'm trying to make this spells work, but I'm having problem with syntax.

Lua:
local time = 20 * 1000 -- 20 seconds


local condition = Condition(CONDITION_ATTRIBUTES)
condition:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTS, gainedHealth)
condition:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
condition:setParameter(CONDITION_PARAM_TICKS, time)

local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, 0)
combat:addCondition(condition)

function onCastSpell(creature, variant, player)
local maxHealth = player:getMaxHealth()
local gainedHealth = (maxHealth / 2) -- how much health the player will get
    combat:execute(creature, variant, player)
    return true
end

Lua Script Error: [Spell Interface]
data/spells/scripts/increasehealth.lua:eek:nCastSpell
data/spells/scripts/increasehealth.lua:15: attempt to index local 'player' (a nil value)
stack traceback:
[C]: in function '__index'
data/spells/scripts/increasehealth.lua:15: in function <data/spells/scripts/increasehealth.lua:14>
I tried to call "player" inside the function, but still not recognizing. Can somebody explain it to me?
 
Solution
Could you show me? As far as I got, it doesn’t recognize the argument player. That’s why I was trying to use it in a function
Lua:
    local buffTime = 3 -- seconds

    local condition = Condition(CONDITION_ATTRIBUTES)
    condition:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
    condition:setParameter(CONDITION_PARAM_TICKS, buffTime * 1000)

    local combat = Combat()
    combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
    combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)

    function onCastSpell(creature, variant)
        local gainedHealth = creature:getMaxHealth() / 2
        condition:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTS, gainedHealth)
        combat:addCondition(condition)...
Try declaring player like
Lua:
local player = Player(cid)
or something like that, I've come across same/similar error before and it did the trick for me.
Hi,

I'm trying to make this spells work, but I'm having problem with syntax.

Lua:
local time = 20 * 1000 -- 20 seconds


local condition = Condition(CONDITION_ATTRIBUTES)
condition:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTS, gainedHealth)
condition:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
condition:setParameter(CONDITION_PARAM_TICKS, time)

local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, 0)
combat:addCondition(condition)

function onCastSpell(creature, variant, player)
local maxHealth = player:getMaxHealth()
local gainedHealth = (maxHealth / 2) -- how much health the player will get
    combat:execute(creature, variant, player)
    return true
end


I tried to call "player" inside the function, but still not recognizing. Can somebody explain it to me?
 
Lua:
function onCastSpell(creature, variant, player)
local maxHealth = player:getMaxHealth()
local gainedHealth = (maxHealth / 2) -- how much health the player will get
    combat:execute(creature, variant, player)
    return true
end
onCastSpell has 2 arguments only (Creature caster, Variant variant), there is no Player argument.
combat:execute has 2 arguments only (Creature caster, Variant variant), there is no Player argument.

Lua:
function onCastSpell(creature, variant)
    local maxHealth = creature:getMaxHealth()
    local gainedHealth = (maxHealth / 2) -- how much health the player will get
    combat:execute(creature, variant)
    return true
end

This will fix your error, won't fix your spell functionality.
 
Try declaring player like
Lua:
local player = Player(cid)
or something like that, I've come across same/similar error before and it did the trick for me.
Thanks, but it didn't work. I probably will have to find another type of function to add this. When I got the answer, I will let you know.
 
Did you try what oen suggested?
Yes, I didn't manage to solve my problem with that:

Lua Script Error: [Spell Interface]
data/spells/scripts/increasehealth.lua:eek:nCastSpell
data/spells/scripts/increasehealth.lua:15: attempt to index local 'caster' (a nil value)
stack traceback:
[C]: in function '__index'
data/spells/scripts/increasehealth.lua:15: in function <data/spells/scripts/increasehealth.lua:14>

I'm trying to use local function, but so far I didn't manage to make it work. No errors appear, the effect appears on the player, but it won't recognize "local gainedHealth". When I change gainedHealth for a number at "condition:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTS, gainedHealth)", it works, but still I would like to use player max health as the amount to increase health. So far I manage to do this:

Lua:
local time = 60 * 1000 -- 60 seconds

local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, 73)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, 0)


local function doPlayerHealth(cid, variant)
local player = Player(cid)
local maxHealth = player:getMaxHealth()
local gainedHealth = (maxHealth / 2) -- how much health the player will get
local condition = Condition(CONDITION_ATTRIBUTES)
condition:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTS, gainedHealth)
condition:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
condition:setParameter(CONDITION_PARAM_TICKS, time)
end

function onCastSpell(creature, variant)
    combat:addCondition(condition)
    combat:execute(creature, variant)
    return true
end
 
Yes, I didn't manage to solve my problem with that:



I'm trying to use local function, but so far I didn't manage to make it work. No errors appear, the effect appears on the player, but it won't recognize "local gainedHealth". When I change gainedHealth for a number at "condition:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTS, gainedHealth)", it works, but still I would like to use player max health as the amount to increase health. So far I manage to do this:

Lua:
local time = 60 * 1000 -- 60 seconds

local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, 73)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, 0)


local function doPlayerHealth(cid, variant)
local player = Player(cid)
local maxHealth = player:getMaxHealth()
local gainedHealth = (maxHealth / 2) -- how much health the player will get
local condition = Condition(CONDITION_ATTRIBUTES)
condition:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTS, gainedHealth)
condition:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
condition:setParameter(CONDITION_PARAM_TICKS, time)
end

function onCastSpell(creature, variant)
    combat:addCondition(condition)
    combat:execute(creature, variant)
    return true
end
You trying to temporarily increase the players max health? or just heal?
 
Could you show me? As far as I got, it doesn’t recognize the argument player. That’s why I was trying to use it in a function
Lua:
    local buffTime = 3 -- seconds

    local condition = Condition(CONDITION_ATTRIBUTES)
    condition:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
    condition:setParameter(CONDITION_PARAM_TICKS, buffTime * 1000)

    local combat = Combat()
    combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
    combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)

    function onCastSpell(creature, variant)
        local gainedHealth = creature:getMaxHealth() / 2
        condition:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTS, gainedHealth)
        combat:addCondition(condition)
        combat:execute(creature, variant)
        return true
    end

I have tested this and it's working on tfs 1.3.
 
Solution
Lua:
local buffTime = 3 -- seconds

local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_GREEN)
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)
   
local condition = Condition(CONDITION_ATTRIBUTES)
condition:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
condition:setParameter(CONDITION_PARAM_TICKS, buffTime * 1000)
condition:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTSPERCENT, 110) -- 110 = 10% buff
combat:addCondition(condition)

function onCastSpell(creature, variant)
    return combat:execute(creature, variant)
end
 
Back
Top