• 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...
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 -_-
If you really are using TFS 1.2 then there is no reason Steve's original post wouldn't work. I've tested his version on original TFS 1.2 and it works as intended.

Based off this:
XML:
    <attacks>
        <attack name=""Kamehameha" interval="9000" min="-10000" max="-12000"/>
    </attacks>

As long as the name of the spell is actually set to Kamehameha and you set direction="1" and not casterTargetOrDirection="1".

If this doesn't work the only reason I can think of is either you are not setting it up correctly or you are using some server that has problems with the way spells are executed for monsters. I suggest you attempt to use his original post once more and triple check that you have everything set up correctly.

If it does not work, here is an alternate way to do it. It's a shame because it is a lot less clean code than Steve's post but it can be your last resort, assuming it even works for your server.

XML:
<attack name="Kamehameha" interval="9000" chance="100"/>

Lua:
local combat = Combat()
combat:setArea(createCombatArea(AREA_BEAM5))

local damage_storage = {}

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(120) end,
    [EAST] = function(p) Position(p.x + 5, p.y, p.z):sendMagicEffect(120) end,
}

local monster_damage = {
    ["Demon"] = {min = 10000, max = 12000}, -- keep min and max as positive numbers, add more if you have other monsters with diff damage
}

function onTargetCreature(creature, target)
    local cid = creature.uid
    doTargetCombatHealth(cid, target, COMBAT_ENERGYDAMAGE, -damage_storage[cid].min, -damage_storage[cid].max, CONST_ME_ENERGYHIT)
end

combat:setCallback(CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(creature, variant)
    local player, cid = creature:getPlayer(), creature.uid
    damage_storage[cid] = {}
    if player then
        local level, maglevel = player:getLevel(), player:getMagicLevel()
        damage_storage[cid].min = (level * 30) + (maglevel * 130.5) + 25
        damage_storage[cid].max = (level * 30) + (maglevel * 140) + 50
    else
        local name = creature:getName()
        damage_storage[cid].min = monster_damage[name].min
        damage_storage[cid].max = monster_damage[name].max
    end

    if combat:execute(creature, variant) then
        direction[ creature:getDirection() ]( creature:getPosition() )
        return true
    end
    return false
end
 
If you really are using TFS 1.2 then there is no reason Steve's original post wouldn't work. I've tested his version on original TFS 1.2 and it works as intended.

Based off this:
XML:
    <attacks>
        <attack name=""Kamehameha" interval="9000" min="-10000" max="-12000"/>
    </attacks>

As long as the name of the spell is actually set to Kamehameha and you set direction="1" and not casterTargetOrDirection="1".

If this doesn't work the only reason I can think of is either you are not setting it up correctly or you are using some server that has problems with the way spells are executed for monsters. I suggest you attempt to use his original post once more and triple check that you have everything set up correctly.

If it does not work, here is an alternate way to do it. It's a shame because it is a lot less clean code than Steve's post but it can be your last resort, assuming it even works for your server.

XML:
<attack name="Kamehameha" interval="9000" chance="100"/>

Lua:
local combat = Combat()
combat:setArea(createCombatArea(AREA_BEAM5))

local damage_storage = {}

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(120) end,
    [EAST] = function(p) Position(p.x + 5, p.y, p.z):sendMagicEffect(120) end,
}

local monster_damage = {
    ["Demon"] = {min = 10000, max = 12000}, -- keep min and max as positive numbers, add more if you have other monsters with diff damage
}

function onTargetCreature(creature, target)
    local cid = creature.uid
    doTargetCombatHealth(cid, target, COMBAT_ENERGYDAMAGE, -damage_storage[cid].min, -damage_storage[cid].max, CONST_ME_ENERGYHIT)
end

combat:setCallback(CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(creature, variant)
    local player, cid = creature:getPlayer(), creature.uid
    damage_storage[cid] = {}
    if player then
        local level, maglevel = player:getLevel(), player:getMagicLevel()
        damage_storage[cid].min = (level * 30) + (maglevel * 130.5) + 25
        damage_storage[cid].max = (level * 30) + (maglevel * 140) + 50
    else
        local name = creature:getName()
        damage_storage[cid].min = monster_damage[name].min
        damage_storage[cid].max = monster_damage[name].max
    end

    if combat:execute(creature, variant) then
        direction[ creature:getDirection() ]( creature:getPosition() )
        return true
    end
    return false
end
Yes your code works
It has direction.
<instant name="Kamehameha" words="kamehameha" lvl="75" maglv="75" mana="1500" prem="0" exhaustion="1000" direction="1" enabled="1" script="LvL75/kamehameha.lua"></instant>

It dont thing there is something wrong executing spell because this spell works just fine with players and monsters
XML:
<instant name="Kiaiho" words="kiaiho" lvl="10" maglv="1" mana="200" prem="0" exhaustion="1000"  script="LvL10/kiaiho.lua">
basically every other spell works either only this spell doesnt work with monsters. About using Steve code like i said 5times already his
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(120) end,
[EAST] = function(p) Position(p.x + 5, p.y, p.z):sendMagicEffect(120) end,
}
doesnt work, if this code doesnt have ability to add custome sprite and adjust x,y,z i lose the whole point of mine skill basically there would be no reason to use it which is example Steve code its not my skill anymore what i provided.
 
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 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 config = {
    [NORTH] = {effectId = 130, x = 0, y = -1},
    [SOUTH] = {effectId = 130, x = 0, y = 5},
    [WEST] = {effectId = 129, x = -1, y = 0},
    [EAST] = {effectId = 129, x = 5, y = 0},
}

function onCastSpell(creature, variant)
    local creatureDirection = creature:getDirection()
    local config = config[creatureDirection]
    if not config then
        return false
    end

    if combat:execute(creature, variant) then
        local creaturePosition = creature:getPosition()
        sendCustomMagicEffect(creaturePosition, config.effectId, config.x, config.y)

        return true
    end

    return false
end
 
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 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 config = {
    [NORTH] = {effectId = 130, x = 0, y = -1},
    [SOUTH] = {effectId = 130, x = 0, y = 5},
    [WEST] = {effectId = 129, x = -1, y = 0},
    [EAST] = {effectId = 129, x = 5, y = 0},
}

function onCastSpell(creature, variant)
    local creatureDirection = creature:getDirection()
    local config = config[creatureDirection]
    if not config then
        return false
    end

    if combat:execute(creature, variant) then
        local creaturePosition = creature:getPosition()
        sendCustomMagicEffect(creaturePosition, config.effectId, config.x, config.y)

        return true
    end

    return false
end
Works perfectly. Thank you very much
 
Back
Top