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

CreatureEvent onTarget Interact with Npc

Script for TFS 0.3.6 is something like this:

Code:
function onTarget(cid, target)

    if isPlayer(cid) and isNpc(target) then
        doPlayerSay(cid,"hi", 1)
        addEvent(doPlayerSay, 500, cid, "trade", 4)
    else
        return true
    end
end

But trade dont work! (Dont know why :s)
Enjoy it ;)

TFS 0.X version

Code:
function getDistanceBetween(fromPosition, toPosition)
   local x, y = math.abs(fromPosition.x - toPosition.x), math.abs(fromPosition.y - toPosition.y)
   local diff = math.max(x, y)
   if(fromPosition.z ~= toPosition.z) then
     diff = diff + 9 + 6
   end
   return diff
end

local minDist = 3

function onTarget(cid, target)
    if (isNpc(target) and getDistanceBetween(getThingPos(cid), getThingPos(target)) <= minDist) then
        doCreatureSay(cid, "hi", TALKTYPE_PRIVATE_PN, false, target)   
        doCreatureSay(cid, "trade", TALKTYPE_PRIVATE_PN, false, target)   
    end
    return not (isNpc(target))
end
 
hello and for tfs 1.3 ?? i try this but dont work only character attack the npc :V

Creature.lua
Lua:
function Creature:onChangeOutfit(outfit)
    return true
end

function Creature:onAreaCombat(tile, isAggressive)
    return RETURNVALUE_NOERROR
end

function Creature:onTargetCombat(target)
    if (self:isPlayer() and target:isNpc()) then
        self:say("hi", TALKTYPE_PRIVATE_PN, false, target)
        self:say("trade", TALKTYPE_PRIVATE_PN, false, target)
        return RETURNVALUE_NOTPOSSIBLE 
    end
 
I found a bug. If you are talking to 1 npc and u click on another then both npc's will have your focus. Is that normal behavior?
 
TFS 0.X version

Code:
function getDistanceBetween(fromPosition, toPosition)
   local x, y = math.abs(fromPosition.x - toPosition.x), math.abs(fromPosition.y - toPosition.y)
   local diff = math.max(x, y)
   if(fromPosition.z ~= toPosition.z) then
     diff = diff + 9 + 6
   end
   return diff
end

local minDist = 3

function onTarget(cid, target)
    if (isNpc(target) and getDistanceBetween(getThingPos(cid), getThingPos(target)) <= minDist) then
        doCreatureSay(cid, "hi", TALKTYPE_PRIVATE_PN, false, target)  
        doCreatureSay(cid, "trade", TALKTYPE_PRIVATE_PN, false, target)  
    end
    return not (isNpc(target))
end

Where should I add this code?
 
Well im pretty fresh in making ots but just switch it to Target function:

Creaturescript.xml:
<event type="Target" name="NpcAttack" event="script" value="npc attack.lua"/>


npc attack.lua:
function getDistanceBetween(fromPosition, toPosition)
local x, y = math.abs(fromPosition.x - toPosition.x), math.abs(fromPosition.y - toPosition.y)
local diff = math.max(x, y)
if(fromPosition.z ~= toPosition.z) then
diff = diff + 9 + 6
end
return diff
end

local minDist = 3

function onTarget(cid, target)
if (isNpc(target) and getDistanceBetween(getThingPos(cid), getThingPos(target)) <= minDist) then
doCreatureSay(cid, "hi", TALKTYPE_PRIVATE_PN, false, target)
doCreatureSay(cid, "trade", TALKTYPE_PRIVATE_PN, false, target)
end
return not (isNpc(target))
end

login.lua
registerCreatureEvent(cid, "NpcAttack")


With this u dont even have to rebuild source to target NPC in protection zone, atleast work on TFS 0,4 w/o editing source ;]
 
Hello it's my source code solution.
PS: don't work on battle list target
Combat.cpp
after this
Code:
    if (player == target) {
        return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
    }
add this
Code:
    if (target || target->getNpc())
    {
        if (player)
        {
            SpectatorHashSet spectators;
            if (target) {
                spectators.insert(target);
            }
            g_game.internalCreatureSay(player, TALKTYPE_PRIVATE_PN, "hi", false, &spectators);
            g_game.internalCreatureSay(player, TALKTYPE_PRIVATE_PN, "trade", false, &spectators);
        }
    }

Im using latest opentibiabr/otservbr-global (https://github.com/opentibiabr/OTServBR-Global)
 
For OTX users, they can simply create a new lua file in data/modules/scripts/file.lua and add this code:
Lua:
local function onSayTradeToNpc(cid, nid)
    local player = Player(cid)
    local target = Npc(nid)
    if player and target then
        player:say('trade', TALKTYPE_PRIVATE_PN, false, target)
    end
end

function onRecvbyte(player, msg, byte)
    local target = Creature(msg:getU32())
    if target ~= nil and target ~= player and target:isNpc() then
        if target:getPosition():getDistance(player:getPosition()) <= 4 then
            player:say('hi', TALKTYPE_PRIVATE_NP, false, target)
            addEvent(onSayTradeToNpc, 350, player.uid, target.uid)
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'Im sorry, these far away the Npc ' .. target:getName())
        end
    end
end

and in the file data/modules/modules.xml add this tag:
XML:
<module type="recvbyte" byte="161" script="file.lua"/>

and it works, without compiling, and regardless of whether it is in a PZ or not.
 
You are using OTClient?
If so, you don't need anything on the engine side.just in the module interface script to give (on a similar principle as you have mod_spacebarattack just to press the mouse posted on npc chat hi)
 
@Beater96 But to install the script i need to change the source files of the tfs...
so how i am supposed to do this without change the source files?

edit:
I read the (space bar mod) files, but I can't find anything that looks like a mouse-like function that i can use
 
Last edited:
on game_interface.lua paste

Lua:
local function doGreetNpc(creatureThing, localPosition)
  if (Position.distance(creatureThing:getPosition(), localPosition) <= 4) then
    scheduleEvent(g_game.talkChannel(11,0,"hi"), 500)
    -- 11 is on 8.6 npc channel :D
  else
    modules.game_textmessage.displayFailureMessage(tr('You are too far away.'))
    end
end

-- next in function processMouseAction(menuPosition, mouseButton, autoWalkPos, lookThing, useThing, creatureThing, attackCreature)
-- add

elseif creatureThing and creatureThing:isNpc() and mouseButton == MouseLeftButton then
      doGreetNpc(creatureThing, g_game.getLocalPlayer():getPosition())
    end

On this place paste
1596234058606.png
 
Back
Top