• 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 [SPELL][TFS 1.2] doesnt ricochet

Tbol

Well-Known Member
Joined
Apr 7, 2019
Messages
529
Reaction score
56
Hi for some reason my spell doesnt ricochet
Lua:
local combats = {}
local spellConfig = {
    combat = COMBAT_ENERGYDAMAGE,
    distanceEffect = CONST_ANI_DEATH,
    effect = CONST_ME_MORTAREA,
    bounces = {
        max = 4,
        chance = 100,
        baseMult = 0.1,
        levelMult = 0.1,
        magicMult = 0.1
    }
}
for i = 1, spellConfig.bounces.max do
    combats[i] = Combat()
    combats[i]:setParameter(COMBAT_PARAM_TYPE, spellConfig.combat)
    combats[i]:setParameter(COMBAT_PARAM_EFFECT, spellConfig.effect)
    function onGetFormulaValues(player, level, magicLevel)
        local min = ((level * (1.0 + (spellConfig.bounces.levelMult * i))) + (magicLevel * (1.0 + (spellConfig.bounces.magicMult * i)))) * (1.0 + (spellConfig.bounces.baseMult * i))
        local max = ((level * (1.2 + (spellConfig.bounces.levelMult * i))) + (magicLevel * (1.2 + (spellConfig.bounces.magicMult * i)))) * (1.2 + (spellConfig.bounces.baseMult * i))
        return -min, -max
    end
    combats[i]:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")
end
local function getClosedTarget(player, cid, targets)
    local targets = targets or {}
    if #targets >= spellConfig.bounces.max then
        return targets
    end
    local c = Creature(cid)
    if c then
        targets[#targets +1] = cid
        local spectators = Game.getSpectators(c:getPosition(), false, false, 4, 4, 4, 4)
        table.sort(spectators, function(a, b) return a:getPosition():getDistance(c:getPosition()) < b:getPosition():getDistance(c:getPosition()) end)
        for _, spectator in pairs(spectators) do
            local sid = spectator:getId()
            if (spectator:isMonster() or (spectator:isPlayer() and not table.contains(targets, sid))) then
                return getClosedTarget(player, sid, targets)
            end
        end
    end
    return targets
end

function onCastSpell(creature, variant)
    local targets = getClosedTarget(creature, variant:getNumber())
    for i, tid in pairs(targets) do
        if i ~= 1 and math.random(100) > spellConfig.bounces.chance then
            break
        end
        addEvent(function(cid, tid, atid)
            local player = Player(cid)
            if player then
                local target = Creature(tid)
                if target then
                    if not atid then
                        player:getPosition():sendDistanceEffect(target:getPosition(), spellConfig.distanceEffect)
                    else
                        local atarget = Creature(atid)
                        if atarget then
                            atarget:getPosition():sendDistanceEffect(target:getPosition(), spellConfig.distanceEffect)
                        end
                    end
                    combats[i]:execute(creature, Variant(tid))
                end
            end
        end, 200 * i-1, creature:getId(), tid, targets[i-1])
    end
    return #targets > 0
end
gif-03-02-2021-12-46-19-a-m-gif.54465

For me it just hits one enemy 4 times
 
Last edited by a moderator:
Tfs 1.2 like i mentioned above

Oh sorry, didn't see that. No errors huh? I think cid was never defined in onCastSpell() and neither was atid, only "tid" was defined when calling addEvent(), the other two variables weren't defined in that scope. That is the problem I believe.

If I'm right, and the rest of the logic is written correctly, could just change "if player then" on line 53 to "if creature:isPlayer()", delete everything on line 52, and add "atid = targets[i+1]" on line 52.

That should have the logic working as intended.
 
Oh sorry, didn't see that. No errors huh? I think cid was never defined in onCastSpell() and neither was atid, only "tid" was defined when calling addEvent(), the other two variables weren't defined in that scope. That is the problem I believe.

If I'm right, and the rest of the logic is written correctly, could just change "if player then" on line 53 to "if creature:isPlayer()", delete everything on line 52, and add "atid = targets[i+1]" on line 52.

That should have the logic working as intended.
so it gives attempt to index global 'player' (a nil value) on line 57
 
Okay no errors but doesnt work still
still just attacking one target? Idk, I guess when I get off work this week I can copy the script and tinker with it, but I'm not off until monday, could you point me to the thread that you got the script from? Did you make any alterations besides the ones I instructed you to make, before posting here?
 
still just attacking one target? Idk, I guess when I get off work this week I can copy the script and tinker with it, but I'm not off until monday, could you point me to the thread that you got the script from? Did you make any alterations besides the ones I instructed you to make, before posting here?
 
it appears you only changed it from a revscripts spell to a normal spell, so undo the changes i had you make before, and make sure it has the proper parameters in the spells.xml, my guess is you forgot

needTarget="1"

or something, if that don't work then share you spells.xml entry, why change it to normal spell anyways, would work fine as a drop in spell in revscripts...
 
it appears you only changed it from a revscripts spell to a normal spell, so undo the changes i had you make before, and make sure it has the proper parameters in the spells.xml, my guess is you forgot

needTarget="1"

or something, if that don't work then share you spells.xml entry, why change it to normal spell anyways, would work fine as a drop in spell in revscripts...
it does have needTarget
XML:
    <instant group="attack" spellid="2" name="test spell" words="test spell" needtarget="1"  direction="1"  lvl="10" maglv="15"    mana="200"    soul="0" exhaustion="1000" prem="0" enabled="1" script="lvl10/test spell.lua">
 
the xml you shared, has it...

I'm not really sure why it won't work for you, did you try just copying original script and dropping it in your data/scripts folder and removing the xml?
 
i dont have data/scripts
Sorry bro, I forgot 1.2 didn't have it yet. I'm working on the solution, believe I discovered the problem, problem is, a group of same kind of monsters all share same creature ID. And Creature ID is what is needed to be passed in an addevent, so its attacking same monster. I think, if you placed five monsters of different types next to each other and tried it, it would work, trying to figure out the solution to this problem right now.
 
@Tbol I can confirm, if you use the original script you shared here, with sarah's fix, it works, however it seems to always take the same path if you have a group of monsters not moving, it will always strike them in same order until one dies.
 
Back
Top