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

attempt to index local 'player' (Spell)

SixNine

Active Member
Joined
Dec 12, 2018
Messages
452
Reaction score
41
35274
So this spell was casted by creature and i noticed that it gave error. Is it because its made for players not for monsters to cast?
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
combat:setArea(createCombatArea(AREA_BEAM5))
 
function onGetFormulaValues(player, level, maglevel)
    local min = (level * 30) + (maglevel * 130.5) + 25
    local max = (level * 30) + (maglevel * 140) + 50
    return -min, -max
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
 
function onCastSpell(cid, var)
    local player = Player(cid)
    local playerPos = player:getPosition()
    local playerDir = player:getDirection()
 
    if playerDir == NORTH then
        Position(playerPos.x, playerPos.y - 1, playerPos.z):sendMagicEffect(130)
    elseif playerDir == SOUTH then
        Position(playerPos.x, playerPos.y + 5, playerPos.z):sendMagicEffect(130)
    elseif playerDir == WEST then
        Position(playerPos.x - 1, playerPos.y, playerPos.z):sendMagicEffect(129)
    elseif playerDir == EAST then
        Position(playerPos.x + 5, playerPos.y, playerPos.z):sendMagicEffect(129)
    end
 
    return combat:execute(cid, var)
end[/cpde]
 
Solution
Lua:
local function sendCustomMagicEffect(pos, magicEffect, x, y)
    if not pos then print("sendCustomMagicEffect: error with pos") return false end
    if not magicEffect then print("sendCustomMagicEffect: error with effect") return false end

    if not x then x = 0 end
    if not y then y = 0 end

    local modifiedPos = Position{x = pos.x + x, y = pos.y + y, z = pos.z}
    local spectators = Game.getSpectators(modifiedPos, false, true, 7, 7, 7, 7)
    for _, spectator in ipairs(spectators) do
        modifiedPos:sendMagicEffect(magicEffect, spectator)
    end   

    return true
end

local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
combat:setArea(createCombatArea(AREA_BEAM5))

function...
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
combat:setArea(createCombatArea(AREA_BEAM5))

function onGetFormulaValues(player, level, maglevel)
    local min = (level * 30) + (maglevel * 130.5) + 25
    local max = (level * 30) + (maglevel * 140) + 50
    return -min, -max
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

function onCastSpell(creature, variant)
    local player = Creature(cid)
    local playerPos = player:getPosition()
    local playerDir = player:getDirection()

    if playerDir == NORTH then
        Position(playerPos.x, playerPos.y - 1, playerPos.z):sendMagicEffect(130)
    elseif playerDir == SOUTH then
        Position(playerPos.x, playerPos.y + 5, playerPos.z):sendMagicEffect(130)
    elseif playerDir == WEST then
        Position(playerPos.x - 1, playerPos.y, playerPos.z):sendMagicEffect(129)
    elseif playerDir == EAST then
        Position(playerPos.x + 5, playerPos.y, playerPos.z):sendMagicEffect(129)
    end

   
    return combat:execute(creature, variant)
end
 
Last edited:
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
combat:setArea(createCombatArea(AREA_BEAM5))

function onGetFormulaValues(player, level, maglevel)
    local min = (level * 30) + (maglevel * 130.5) + 25
    local max = (level * 30) + (maglevel * 140) + 50
    return -min, -max
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

function onCastSpell(cid, var)
    local player = Creature(cid)
    local playerPos = player:getPosition()
    local playerDir = player:getDirection()

    if playerDir == NORTH then
        Position(playerPos.x, playerPos.y - 1, playerPos.z):sendMagicEffect(130)
    elseif playerDir == SOUTH then
        Position(playerPos.x, playerPos.y + 5, playerPos.z):sendMagicEffect(130)
    elseif playerDir == WEST then
        Position(playerPos.x - 1, playerPos.y, playerPos.z):sendMagicEffect(129)
    elseif playerDir == EAST then
        Position(playerPos.x + 5, playerPos.y, playerPos.z):sendMagicEffect(129)
    end

    return combat:execute(cid, var)
end
So it wont work with players now right?
 
Try this, obviously not tested :p
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
combat:setArea(createCombatArea(AREA_BEAM5))

function onGetFormulaValues(player, level, maglevel)
    local min = (level * 30) + (maglevel * 130.5) + 25
    local max = (level * 30) + (maglevel * 140) + 50
    return -min, -max
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

local direction = {
    [NORTH] = function(p) Position(p.x, p.y - 1, p.z):sendMagicEffect(130) end,
    [SOUTH] = function(p) Position(p.x, p.y + 5, p.z):sendMagicEffect(130) end,
    [WEST] = function(p) Position(p.x - 1, p.y, p.z):sendMagicEffect(129) end,
    [EAST] = function(p) Position(p.x + 5, p.y, p.z):sendMagicEffect(129) end,
}

function onCastSpell(creature, variant)
    direction[ creature:getDirection() ]( creature:getPosition() )
    return combat:execute(creature, variant)
end
 
Try this, obviously not tested :p
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
combat:setArea(createCombatArea(AREA_BEAM5))

function onGetFormulaValues(player, level, maglevel)
    local min = (level * 30) + (maglevel * 130.5) + 25
    local max = (level * 30) + (maglevel * 140) + 50
    return -min, -max
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

local direction = {
    [NORTH] = function(p) Position(p.x, p.y - 1, p.z):sendMagicEffect(130) end,
    [SOUTH] = function(p) Position(p.x, p.y + 5, p.z):sendMagicEffect(130) end,
    [WEST] = function(p) Position(p.x - 1, p.y, p.z):sendMagicEffect(129) end,
    [EAST] = function(p) Position(p.x + 5, p.y, p.z):sendMagicEffect(129) end,
}

function onCastSpell(creature, variant)
    direction[ creature:getDirection() ]( creature:getPosition() )
    return combat:execute(creature, variant)
end
No errors everything is clean player can cast spell but monster cant cast this spell at all it doesnt give errors
XML:
<attack name="Kamehameha" interval="9000" min="-10000" max="-12000"/>
 
No errors everything is clean player can cast spell but monster cant cast this spell at all it doesnt give errors
XML:
<attack name="Kamehameha" interval="9000" min="-10000" max="-12000"/>
I think you might have to add it to spells.xml as a monster spell at the bottom of spells.xml is all the monster spells I am not sure about this tho. Try it.
 
I think you might have to add it to spells.xml as a monster spell at the bottom of spells.xml is all the monster spells I am not sure about this tho. Try it.
You mean like?
<vocation name="Monster name"/>
or i dont know what you mean

EDIT: but i dont think so, because it does like target and area spells it just doesnt do this spell for some reason
 
Okay, thank you.
Ok so after realizing there is no magic effect of 129/130 I go it working for a monster and a player & I re-wrote it some.
XML:
<!--
this would go inside of the attacks node of your monster
interval is how often it executes 100 would mean every 10th of a second
chance is a matter of how often it is successful at executing
and range would be the distance between you and the target
name implies the name of the spell found in spells.xml
-->
<attack name="monster test" interval="100" chance="100" range="1" />

XML:
<!--
this is the part that goes in spells.xml for the monster
words tell the server which spell id to use for that monster
the needlearn attribute is important without it anyone could cast this spell
the direction attribute is used with the callback function (onTargetCreature)
script would be the path to & including the lua file
-->
<instant name="monster test" words="###51" direction="1" needlearn="1" script="test.lua" />
You can also use casterTargetOrDirection instead of direction or possibly use both. The difference I noticed is that direction requires (for this script) that the attacker is facing its target in alignment where as casterTargetOrDirection just needs to be facing the target (as if it were normally attacking it)

So this is the new script that works with the above settings in spells & whatever you use this for.
Lua:
--[[
    CONST_ME_REDSMOKE = 167,
    CONST_ME_YELLOWSMOKE = 168,
    CONST_ME_GREENSMOKE = 169,
    CONST_ME_PURPLESMOKE = 170,

]]
local direction = {
    [NORTH] = function(p) Position(p.x, p.y - 1, p.z):sendMagicEffect(167) end,
    [SOUTH] = function(p) Position(p.x, p.y + 1, p.z):sendMagicEffect(168) end,
    [WEST] = function(p) Position(p.x - 1, p.y, p.z):sendMagicEffect(169) end,
    [EAST] = function(p) Position(p.x + 1, p.y, p.z):sendMagicEffect(170) end,
}

local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_ENERGYHIT)
combat:setArea(createCombatArea(AREA_BEAM5, AREADIAGONAL_BEAM5))

function onTargetCreature(creature, target)
    local player = creature:getPlayer()
    local min, max = -1, -10
    if player then
        local level, maglevel = player:getLevel(), player:getMagicLevel()
        min = -((level * 30) + (maglevel * 130.5) + 25)
        max = -((level * 30) + (maglevel * 140) + 50)
    end

    direction[ creature:getDirection() ]( creature:getPosition() )
    doTargetCombatHealth(creature:getId(), target, COMBAT_ENERGYDAMAGE, min, max, CONST_ME_TELEPORT)
    return true
end

combat:setCallback(CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(creature, variant)
    return combat:execute(creature, variant)
end
The script is a combination of mass healing, & great energy beam along with aspects of the original script posted on this forum.
The rest you'll need to experiment with and learn on your own. The important thing here is that you now have an idea (me as well) how to assign scripts to a monster and not just players.

On a final note I am not concerned with the scripts contents nor its execution because I didn't come up with the idea for sending the effects to specific positions nor using a variation of great energy beam.
 
Last edited:
Ok so after realizing there is no magic effect of 129/130 I go it working for a monster and a player & I re-wrote it some.
XML:
<!--
this would go inside of the attacks node of your monster
interval is how often it executes 100 would mean every 10th of a second
chance is a matter of how often it is successful at executing
and range would be the distance between you and the target
name implies the name of the spell found in spells.xml
-->
<attack name="monster test" interval="100" chance="100" range="1" />

XML:
<!--
this is the part that goes in spells.xml for the monster
words tell the server which spell id to use for that monster
the needlearn attribute is important without it anyone could cast this spell
the direction attribute is used with the callback function (onTargetCreature)
script would be the path to & including the lua file
-->
<instant name="monster test" words="###51" direction="1" needlearn="1" script="test.lua" />
You can also use casterTargetOrDirection instead of direction or possibly use both. The difference I noticed is that direction requires (for this script) that the attacker is facing its target in alignment where as casterTargetOrDirection just needs to be facing the target (as if it were normally attacking it)

So this is the new script that works with the above settings in spells & whatever you use this for.
Lua:
--[[
    CONST_ME_REDSMOKE = 167,
    CONST_ME_YELLOWSMOKE = 168,
    CONST_ME_GREENSMOKE = 169,
    CONST_ME_PURPLESMOKE = 170,

]]
local direction = {
    [NORTH] = function(p) Position(p.x, p.y - 1, p.z):sendMagicEffect(167) end,
    [SOUTH] = function(p) Position(p.x, p.y + 1, p.z):sendMagicEffect(168) end,
    [WEST] = function(p) Position(p.x - 1, p.y, p.z):sendMagicEffect(169) end,
    [EAST] = function(p) Position(p.x + 1, p.y, p.z):sendMagicEffect(170) end,
}

local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_ENERGYHIT)
combat:setArea(createCombatArea(AREA_BEAM5, AREADIAGONAL_BEAM5))

function onTargetCreature(creature, target)
    local player = creature:getPlayer()
    local min, max = -1, -10
    if player then
        local level, maglevel = player:getLevel(), player:getMagicLevel()
        min = -((level * 30) + (maglevel * 130.5) + 25)
        max = -((level * 30) + (maglevel * 140) + 50)
    end

    direction[ creature:getDirection() ]( creature:getPosition() )
    doTargetCombatHealth(creature:getId(), target, COMBAT_ENERGYDAMAGE, min, max, CONST_ME_TELEPORT)
    return true
end

combat:setCallback(CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(creature, variant)
    return combat:execute(creature, variant)
end
The script is a combination of mass healing, & great energy beam along with aspects of the original script posted on this forum.
The rest you'll need to experiment with and learn on your own. The important thing here is that you now have an idea (me as well) how to assign scripts to a monster and not just players.

On a final note I am not concerned with the scripts contents nor its execution because I didn't come up with the idea for sending the effects to specific positions nor using a variation of great energy beam.
Hmm whats the point to use local direction when you change effect id, it doesnt change in game, basically local direction do not work
 
Hmm whats the point to use local direction when you change effect id, it doesnt change in game, basically local direction do not work
direction is a table of directions the player could possibly face. local is a keyword in lua to tell the server/interpreter its scope. Since it is defined outside in the open. Meaning not within a Main method or function then its scope is local to that of the script but global to everything else that resides in it.
 
direction is a table of directions the player could possibly face. local is a keyword in lua to tell the server/interpreter its scope. Since it is defined outside in the open. Meaning not within a Main method or function then its scope is local to that of the script but global to everything else that resides in it.
Okay so why they have effect id when they dont work (sendMagicEffect(170)) ? And why it needs spell path when every other spell works just area beam doesnt so it doesnt make sense to me why path is needed, and range="1" is it effect range or range needed to activate that spell?
 
Okay so why they have effect id when they dont work (sendMagicEffect(170)) ? And why it needs spell path when every other spell works just area beam doesnt so it doesnt make sense to me why path is needed, and range="1" is it effect range or range needed to activate that spell?
What path? If you're referring to the comment of of the path then it means the path to the file. Other than that I have no clue what you're talking about.

The script works I just think that you don't know how to follow instructions. That's ok tho because I've waisted too much time in this thread & I could be doing other things.
 
What path? If you're referring to the comment of of the path then it means the path to the file. Other than that I have no clue what you're talking about.

The script works I just think that you don't know how to follow instructions. That's ok tho because I've waisted too much time in this thread & I could be doing other things.
Like i said why does it need path when it can detect spell automatically just by name? About this part
Lua:
local direction = {
    [NORTH] = function(p) Position(p.x, p.y - 1, p.z):sendMagicEffect(167) end,
    [SOUTH] = function(p) Position(p.x, p.y + 1, p.z):sendMagicEffect(168) end,
    [WEST] = function(p) Position(p.x - 1, p.y, p.z):sendMagicEffect(169) end,
    [EAST] = function(p) Position(p.x + 1, p.y, p.z):sendMagicEffect(170) end,
}
which doesnt work tried changing effect ids it always stays
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_ENERGYHIT)
so like why do you making COMBAT_PARAM_EFFECT and then adding sendMagicEffect on local it doesnt make sense and it doesnt work. I tested it.
 
Like i said why does it need path when it can detect spell automatically just by name? About this part
Lua:
local direction = {
    [NORTH] = function(p) Position(p.x, p.y - 1, p.z):sendMagicEffect(167) end,
    [SOUTH] = function(p) Position(p.x, p.y + 1, p.z):sendMagicEffect(168) end,
    [WEST] = function(p) Position(p.x - 1, p.y, p.z):sendMagicEffect(169) end,
    [EAST] = function(p) Position(p.x + 1, p.y, p.z):sendMagicEffect(170) end,
}
which doesnt work tried changing effect ids it always stays
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_ENERGYHIT)
so like why do you making COMBAT_PARAM_EFFECT and then adding sendMagicEffect on local it doesnt make sense and it doesnt work. I tested it.
The entire script never made any sense let alone work. When I ran it on the server the one which I initially made corrections to the one you said worked which did not on mine with those magic effect values you used and the +5 position increments. I had a hard time understanding exactly what was the point of this script that you threw together copy and pasting code from elsewhere. Then you wanted it to work with monsters aswell so I made a decision to alter it's state and make some sense out of your code so that it did work for both players & monster.

In short you're welcome.
 
The entire script never made any sense let alone work. When I ran it on the server the one which I initially made corrections to the one you said worked which did not on mine with those magic effect values you used and the +5 position increments. I had a hard time understanding exactly what was the point of this script that you threw together copy and pasting code from elsewhere. Then you wanted it to work with monsters aswell so I made a decision to alter it's state and make some sense out of your code so that it did work for both players & monster.

In short you're welcome.
I dont remember who made it but he had knowledge with coding. The whole point of those +5 if is that i use custome sprites which is huge not like 32x32 its more like 160+ so without those x,y,z i cant align sprite thats why it didnt worked in your server. And now it doesnt work in mine -_-
 
Back
Top