• 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 While targetting x monster

ohman

Member
Joined
Oct 24, 2008
Messages
287
Reaction score
5
Location
Sweden
Hello!

I'm working on a script to determine if the player is targeting a specific monster. Currently, my script can detect when the attack on a monster begins, but it doesn't recognize when the attack stops. Does anyone have any ideas on how to do this? Thanks in advance!

Lua:
local ec = EventCallback

function ec.onTargetCombat(creature, target)
print(target:getName())
    return RETURNVALUE_NOERROR
end

ec:register(38651)
 
This way you can identify when the player start attack and stop attack with specific creatures

Lua:
local function onStartCombat(player, target)
    -- YOUR CODE
end

local function onStopCombat(player, target)
    -- YOUR CODE
end

----------------------------------------------
local eventIds = {}

function checkOnChangeTarget(playerId, targetId)
    local player = Player(playerId)
    if not player then return end
    local target = Monster(targetId)
    if not target or player:getTarget() ~= target then
        if target then target:setStorageValue(playerId, nil) end
        return onStopCombat(player, target)
    end
    eventIds[playerId] = addEvent(checkOnChangeTarget, 100, playerId, targetId)
end

local event = EventCallback

function event.onTargetCombat(creature, target)
    if not creature or not creature:isPlayer() or not target or not target:isMonster() then
        return RETURNVALUE_NOERROR
    end

    local creatureId = creature:getId()
    local attacker = target:getStorageValue(creatureId)
    if not attacker then
        local currTarget = creature:getTarget()
        if currTarget then
            stopEvent(eventIds[creatureId])
            currTarget:setStorageValue(creatureId, nil)
            onStopCombat(creature, currTarget)
        end
        target:setStorageValue(creatureId, 1)
        eventIds[creatureId] = addEvent(checkOnChangeTarget, 100, creatureId, target:getId())
    end
    return RETURNVALUE_NOERROR
end

event:register()

OLD TFS versions:
Lua:
local function onStartCombat(player, target)
    -- YOUR CODE
end

local function onStopCombat(player, target)
    -- YOUR CODE
end

----------------------------------------------
local eventIds = {}

function checkOnChangeTarget(playerId, targetId)
    local player = Player(playerId)
    if not player then return end
    local target = Monster(targetId)
    if not target or player:getTarget() ~= target then
        if target then target:setStorageValue(playerId, -1) end
        return onStopCombat(player, target)
    end
    eventIds[playerId] = addEvent(checkOnChangeTarget, 100, playerId, targetId)
end

local event = EventCallback

function event.onTargetCombat(creature, target)
    if not creature or not creature:isPlayer() or not target or not target:isMonster() then
        return RETURNVALUE_NOERROR
    end

    local creatureId = creature:getId()
    local attacker = target:getStorageValue(creatureId)
    if attacker ~= -1 then
        local currTarget = creature:getTarget()
        if currTarget then
            stopEvent(eventIds[creatureId])
            currTarget:setStorageValue(creatureId, -1)
            onStopCombat(creature, currTarget)
        end
        target:setStorageValue(creatureId, 1)
        eventIds[creatureId] = addEvent(checkOnChangeTarget, 100, creatureId, target:getId())
    end
    return RETURNVALUE_NOERROR
end

event:register()


It could be done better by modifying the source code to add a new onStopCombat method or something like that but I don't really know what you want.
 
Last edited:
Another option would be to have the onTargetCombat trigger/register/turn on an onThink event for the specific monster, have your on think doing your logic (does so every 1000 miliseconds, so every one second), and in that logic, have it check if it is still targeting the player... or perhaps even more simple, have it trigger/register/turn on an "onDisappear" event that will do what you want when the target player disappears from the monsters vision/aggro range.
 
Another option would be to have the onTargetCombat trigger/register/turn on an onThink event for the specific monster, have your on think doing your logic (does so every 1000 miliseconds, so every one second), and in that logic, have it check if it is still targeting the player... or perhaps even more simple, have it trigger/register/turn on an "onDisappear" event that will do what you want when the target player disappears from the monsters vision/aggro range.
onThink with 1 second delay is too slow.
using onDisappear would not work as one would expect, since it only activates in certain circumstances and not exactly when the player stops targeting it.
The best option would be to use the bool Player::setAttackedCreature(Creature* creature) method or instead anchor a lua event for convenience.

Anyway, if you don't want to complicate your life, use the script I wrote, it works perfectly, and the check is only checked as long as the player has a set objective,
otherwise there is no infinite addEvent
 
How to make monster make double hit or use spell twice in less than a second? Would like hunter shoot double arrow as spell in less than a second
Post automatically merged:

solved i made an spell might be not the best aproach but it works
 
Last edited:
Back
Top