• 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 Change Target Spell (TFS 1.2)

demon088

#088 in the Horde
Joined
Jun 17, 2009
Messages
254
Solutions
3
Reaction score
31
Location
Hell
Hello OTLand!
I'm looking for a spell able to force change target on creatures.
EXAMPLE (Of course this code doesn't work):
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)
combat:setArea(createCombatArea(AREA_SQUARE1X1))

function onTargetCreature(creature, target)
    return doChallengeCreature(creature, target)
end

combat:setCallback(CALLBACK_PARAM_TARGETCREATURE, "unTargetCreature")<<<<<<<<<

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

I was thinking in something like the 'Challenge' spell. The spell would work instead of attracting creatures, it would force creatures to change their target randomly. What do you think? I was searching for a similar spell in the Search Bar but I could not find any track of it.
Thanks for your help!

Srry but... BUMP! I really need this.
 
Last edited by a moderator:
you can use Game.getSpectators() then you use some script to check what player you want set target with the function setTarget(), you can use a random chance to get the target or select the highter level, or lower level, everything is possible :rolleyes:
 
you can use Game.getSpectators() then you use some script to check what player you want set target with the function setTarget(), you can use a random chance to get the target or select the highter level, or lower level, everything is possible :rolleyes:

Could you give me an example of a script? I didn't get the reference very well...
Thanks for your help!
 
If you don't know how to convert to 1.2 asks someone.
Code:
-- puts all players around pos to table 'targets'
local list = getSpectators(pos, 10, 10) -- max 10 sqm away from pos
local targets = {}
if list ~= nil and #list > 0 then -- checks if there's no creatures around
    for i = 1, #list do
        if isPlayer(list[i]) == true then -- if is player, adds him to table 'targets'
            table.insert(targets, list[i])
        end
    end
end

-- chooses random target from table 'targets'
local newRandomTarget = 0
if targets ~= nil and #targets > 0 then -- checks if there's no players around (in table 'targets')
    newRandomTarget = targets[math.random(1, #targets)]
end
 
Last edited:
If you don't know how to convert to 1.2 asks someone.
Code:
-- puts all players around pos to table 'targets'
local list = getSpectators(pos, 10, 10) -- max 10 sqm away from pos
local targets = {}
if list ~= nil and #list > 0 then -- checks if there's no creatures around
    for i = 1, #list do
        if isPlayer(list[i]) == true then -- if is player, adds him to table 'targets'
            table.insert(targets, list[i])
        end
    end
end

-- chooses random target from table 'targets'
local newRandomTarget = 0
if targets ~= nil and #targets > 0 then -- checks if there's no players around (in table 'targets')
    newRandomTarget = targets[math.random(1, #targets)]
end
Code:
-- puts all players around pos to table 'targets'
local list = Game.getSpectators(pos, false, true, 0, 10, 0, 10) -- max 10 sqm away from pos
local targets = {}
if #list > 0 then -- checks if there's no creatures around
    for i = 1, #list do
        table.insert(targets, list[i])
    end
end

-- chooses random target from table 'targets'
local newRandomTarget = 0
if #targets > 0 then -- checks if there's no players around (in table 'targets')
    newRandomTarget = targets[math.random(#targets)]
end
 
Code:
-- puts all players around pos to table 'targets'
local list = Game.getSpectators(pos, false, true, 0, 10, 0, 10) -- max 10 sqm away from pos
local targets = {}
if #list > 0 then -- checks if there's no creatures around
    for i = 1, #list do
        table.insert(targets, list[i])
    end
end

-- chooses random target from table 'targets'
local newRandomTarget = 0
if #targets > 0 then -- checks if there's no players around (in table 'targets')
    newRandomTarget = targets[math.random(#targets)]
end
Thanks guys I'll be testing this script soon. Thanks for the help I hope it works. :P

I didn't know how to use that on a spell script...
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)

function onTargetCreature(creature, target)
    return
local list = getSpectators(pos, 10, 10) 
local targets = {}
if list ~= nil and #list > 0 then -- checks if there's no creatures around
    for i = 1, #list do
        if isPlayer(list[i]) == true then -- if is player, adds him to table 'targets'
            table.insert(targets, list[i])
        end
    end
end
local newRandomTarget = 0
if targets ~= nil and #targets > 0 then
    newRandomTarget = targets[math.random(1, #targets)]
end

doChallengeCreature(creature, newRandomTarget)
end
combat:setCallback(CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(creature, variant)
    return combat:execute(creature, variant)
end
I got stuck with the code. xS
 
Last edited by a moderator:
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)

function onCastSpell(creature, variant)
    local list = Game.getSpectators(pos, false, true, 0, 10, 0, 10) -- max 10 sqm away from pos
    local targets = {}
    if list ~= nil and #list > 0 then -- checks if there's no creatures around
        for i = 1, #list do
            if isPlayer(list[i]) == true then -- if is player, adds him to table 'targets'
                table.insert(targets, list[i])
            end
        end
    end
    local newRandomTarget = 0
    if targets ~= nil and #targets > 0 then
        newRandomTarget = targets[math.random(1, #targets)]
    end
    doChallengeCreature(creature, newRandomTarget) -- this is from 0.4, idk how this function looks like in 1.0
    return combat:execute(creature, variant)
end

Important, do you want to make a spell which forces change target of your target?
Or, you want a monster spell what executes 'change target'?
 
He basically wants a spell that forces a target, to change targets.

I don't use TFS 1.X, But couldn't you just use searchTarget.

For example.
Code:
function onCastSpell(cid, variant)
  local target = getCreatureTarget(cid)
  if isMonster(target) then
   target:searchTarget()
  end
  return true
end

(I'm sure someone that is more familiar with TFS 1.X could fix it, but this "should" work. It will run the searchTarget script again.
 
Hello again!
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)

function onCastSpell(creature, variant)
    local list = Game.getSpectators(pos, false, true, 0, 10, 0, 10) -- max 10 sqm away from pos
    local targets = {}
    if list ~= nil and #list > 0 then -- checks if there's no creatures around
        for i = 1, #list do
            if isPlayer(list[i]) == true then -- if is player, adds him to table 'targets'
                table.insert(targets, list[i])
            end
        end
    end
    local newRandomTarget = 0
    if targets ~= nil and #targets > 0 then
        newRandomTarget = targets[math.random(1, #targets)]
    end
    doChallengeCreature(creature, newRandomTarget) -- this is from 0.4, idk how this function looks like in 1.0
    return combat:execute(creature, variant)
end

Important, do you want to make a spell which forces change target of your target?
Or, you want a monster spell what executes 'change target'?

I need a spell that forces monsters or players to change the target to another one. But if there is no target it just won't work. That script is not working at all, but thanks for the help.
Code:
    doChallengeCreature(creature, newRandomTarget) -- this is from 0.4, idk how this function looks like in 1.0
This can be used in TFS 1.2 as well, the challenge spell is working like this.

He basically wants a spell that forces a target, to change targets.

I don't use TFS 1.X, But couldn't you just use searchTarget.

For example.
Code:
function onCastSpell(cid, variant)
  local target = getCreatureTarget(cid)
  if isMonster(target) then
   target:searchTarget()
  end
  return true
end

(I'm sure someone that is more familiar with TFS 1.X could fix it, but this "should" work. It will run the searchTarget script again.
I think I haven't this compiled function. The code you provided is not working for me. I'm thinking that I need compilation for this kind of spell script.
Thanks for all the help guys!
 
Hello again!


I need a spell that forces monsters or players to change the target to another one. But if there is no target it just won't work. That script is not working at all, but thanks for the help.
Code:
    doChallengeCreature(creature, newRandomTarget) -- this is from 0.4, idk how this function looks like in 1.0
This can be used in TFS 1.2 as well, the challenge spell is working like this.


I think I haven't this compiled function. The code you provided is not working for me. I'm thinking that I need compilation for this kind of spell script.
Thanks for all the help guys!

I don't use TFS 1.X so the code I provides probably isn't working correctly.

But the searchTarget function SHOULD be available.
Can you let me know what error you get when using the spell?
 
I need a spell that forces monsters or players to change the target to another one.

It may work only for monsters. You can't change other player's target with LUA without server and client networking changes.
It's just impossible with official releases of TFS and with RL Tibia client atm.
 
Last edited:
I don't use TFS 1.X so the code I provides probably isn't working correctly.

But the searchTarget function SHOULD be available.
Can you let me know what error you get when using the spell?
This is my issue:
Code:
Lua Script Error: [Spell Interface]
data/spells/scripts/support/unchallenge.lua:onCastSpell
data/spells/scripts/support/unchallenge.lua:14: attempt to index local 'target' (a number value)
stack traceback:
        [C]: in function '__index'
        data/spells/scripts/support/unchallenge.lua:14: in function <data/spells/scripts/support/unchallenge.lua:11>
My lua:
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)
combat:setArea(createCombatArea(AREA_SQUARE1X1))

function onTargetCreature(creature, target)
    return doChallengeCreature(creature, target)
end

combat:setCallback(CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onCastSpell(cid, variant)
  local target = getCreatureTarget(cid)
  if isMonster(target) then
   target:searchTarget()
  end
  return true
end
It may work only for monsters. You can't change other player's target with LUA without server and client networking changes.
It's just impossible with official releases of TFS and with RL Tibia client atm.
I understand, I will focus on force monsters to change their targets. I get this issue with your script:
Code:
Lua Script Error: [Spell Interface]
data/spells/scripts/support/unchallenge.lua:onCastSpell
attempt to index a nil value
stack traceback:
        [C]: in ?
        [C]: in function 'getSpectators'
        data/spells/scripts/support/unchallenge.lua:6: in function <data/spells/scripts/support/unchallenge.lua:5>
 
Last edited:
You are getting such errors becouse you set wrong options in your spells.xml
What do you say?
Code:
    <instant group="support" spellid="93" name="Unchallenge" words="exeta ani" lvl="10" mana="30" prem="0" aggressive="0" cooldown="1000" groupcooldown="1000" needlearn="0" script="support/unchallenge.lua">
        <vocation name="Summoner" />
        <vocation name="Soul Linker" />
    </instant>
 
This spell must have needtarget="1"

Also there's no combat, so remove these useless lines:
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)
combat:setArea(createCombatArea(AREA_SQUARE1X1))

function onTargetCreature(creature, target)
   return doChallengeCreature(creature, target)
end

combat:setCallback(CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")
 
Last edited:
I'm sorry for answering so long... School is very heavy right now xP

I tried adding needtarget="1" and I got these:
Code:
Lua Script Error: [Spell Interface]
data/spells/scripts/support/unchallenge.lua:onCastSpell
data/spells/scripts/support/unchallenge.lua:4: attempt to index local 'target' (a number value)
stack traceback:
        [C]: in function '__index'
        data/spells/scripts/support/unchallenge.lua:4: in function <data/spells/scripts/support/unchallenge.lua:1>

My spell script:
LUA:
function onCastSpell(cid, variant)
  local target = getCreatureTarget(cid)
  if isMonster(target) then
   target:searchTarget()
  end
  return true
end
This is how appears on spells.xml
LUA:
    <instant group="support" spellid="93" name="Unchallenge" words="exeta ani" lvl="10" mana="30" prem="0" needtarget="1" aggressive="0" cooldown="1000" groupcooldown="1000" needlearn="0" script="support/unchallenge.lua">
        <vocation name="Summoner" />
        <vocation name="Soul Linker" />
    </instant>
 
Back
Top