• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua script error "getDistanceToCreature"

henkas

Well-Known Member
Joined
Jul 8, 2015
Messages
1,067
Solutions
5
Reaction score
63
Untitled.png

How to fix it. I'm using TFS 1.2
Code:
local focus = 0
local talk_start = 0
local target = 0
local following = false
local attacking = false
local pos = {x=99, y=188, z=7}

function onThingMove(creature, thing, oldpos, oldstackpos)

end


function onCreatureAppear(creature)

end


function onCreatureDisappear(cid, pos)
      if focus == cid then
          selfSay('Hey!.')
          focus = 0
          talk_start = 0
      end
end


function onCreatureTurn(creature)

end


function msgcontains(txt, str)
      return (string.find(txt, str) and not string.find(txt, '(%w+)' .. str) and not string.find(txt, str .. '(%w+)'))
end


function onCreatureSay(cid, type, msg)
      msg = string.lower(msg)

      if (msgcontains(msg, 'hi') and (focus == 0)) and getDistanceToCreature(cid) < 5 then
                doTeleportThing(cid, pos)
                    selfSay('Adeus!')
                    focus = 0
                    talk_start = 0

      elseif msgcontains(msg, 'hi') and (focus ~= cid) and getDistanceToCreature(cid) < 5 then
          selfSay('Sorry, ' .. getCreatureName(cid) .. '! ????????.')

      elseif focus == cid then
        talk_start = os.clock()
               

        elseif msgcontains(msg, 'bye') and getDistanceToCreature(cid) < 5 then
            selfSay('Good bye, ' .. getCreatureName(cid) .. '!')
            focus = 0
            talk_start = 0
        end
    end


function onThink()
    doNpcSetCreatureFocus(focus)
      if (os.clock() - talk_start) > 45 then
          if focus > 0 then
              selfSay('Next Please...')
          end
              focus = 0
      end
    if focus ~= 0 then
        if getDistanceToCreature(focus) > 6 then
            selfSay('Good bye then.')
            focus = 0
        end
    end
end
 
Solution
I want when i write "hi" he teleport me to temple, that's it.
First do this part in xml:
XML:
<parameter key="message_greet" value="Adeus!"/>

Then use this if you want to have a specific temple for all players.
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)              npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)           npcHandler:onCreatureDisappear(cid)         end
function onCreatureSay(cid, type, msg)      npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                          npcHandler:onThink()                        end

local pos =...
Old is bad? :D So yea i added getDistance but now my console flood this message
Untitled.png

Flood every sec.
Yeah it's kinda pointless in this situation too. doNpcSetCreatureFocus works with setNpcFocus which is a source code edit. If you want to mess with it further it's here:
avesta/npc.cpp at 4be0d85d302c9097f02f05ead438c47e2bf4db06 · tarantonio/avesta · GitHub

But really it's best to try do it with the new functions of 1.2. If you explain what you want the npc to do it would be better to get help on rewriting it for the current version instead of adding all these old functions to make it work.

It seems like all this script is going to do teleport to a new position when you say hi, otherwise it's just a bunch of old ways for greetings and walkaway messages, but explain what you need.
 
Yeah it's kinda pointless in this situation too. doNpcSetCreatureFocus works with setNpcFocus which is a source code edit. If you want to mess with it further it's here:
avesta/npc.cpp at 4be0d85d302c9097f02f05ead438c47e2bf4db06 · tarantonio/avesta · GitHub

But really it's best to try do it with the new functions of 1.2. If you explain what you want the npc to do it would be better to get help on rewriting it for the current version instead of adding all these old functions to make it work.

It seems like all this script is going to do teleport to a new position when you say hi, otherwise it's just a bunch of old ways for greetings and walkaway messages, but explain what you need.
I want when i write "hi" he teleport me to temple, that's it.
 
I want when i write "hi" he teleport me to temple, that's it.
First do this part in xml:
XML:
<parameter key="message_greet" value="Adeus!"/>

Then use this if you want to have a specific temple for all players.
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)              npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)           npcHandler:onCreatureDisappear(cid)         end
function onCreatureSay(cid, type, msg)      npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                          npcHandler:onThink()                        end

local pos = Position(95, 117, 7)

local function greetCallback(cid)
    local player = Player(cid)
    if player then
        player:teleportTo(pos)
        pos:sendMagicEffect(CONST_ME_TELEPORT)
    end
    return true
end
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:addModule(FocusModule:new())

Or use this if you want them to go to their home town temple.
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)              npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)           npcHandler:onCreatureDisappear(cid)         end
function onCreatureSay(cid, type, msg)      npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                          npcHandler:onThink()                        end

local function greetCallback(cid)
    local player = Player(cid)
    if player then
        local pos = player:getTown():getTemplePosition()
        player:teleportTo(pos)
        pos:sendMagicEffect(CONST_ME_TELEPORT)
    end
    return true
end
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
First do this part in xml:
XML:
<parameter key="message_greet" value="Adeus!"/>

Then use this if you want to have a specific temple for all players.
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)              npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)           npcHandler:onCreatureDisappear(cid)         end
function onCreatureSay(cid, type, msg)      npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                          npcHandler:onThink()                        end

local pos = Position(95, 117, 7)

local function greetCallback(cid)
    local player = Player(cid)
    if player then
        player:teleportTo(pos)
        pos:sendMagicEffect(CONST_ME_TELEPORT)
    end
    return true
end
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:addModule(FocusModule:new())

Or use this if you want them to go to their home town temple.
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)              npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)           npcHandler:onCreatureDisappear(cid)         end
function onCreatureSay(cid, type, msg)      npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                          npcHandler:onThink()                        end

local function greetCallback(cid)
    local player = Player(cid)
    if player then
        local pos = player:getTown():getTemplePosition()
        player:teleportTo(pos)
        pos:sendMagicEffect(CONST_ME_TELEPORT)
    end
    return true
end
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:addModule(FocusModule:new())
Very nice work great. But i have question when i done teleporting he send message in NPC channel [MESSAGE_GREET] = "Greetings, |PLAYERNAME|.",
how can i delete this this part because he send this message really slowly like only per 4seconds so i want to avoid sending this message and attach new message when i type "hi" he says "NPCName:hi "PlayerName" and when he teleport me he says "NPCName:lets go" how can i make something like this?
 
Very nice work great. But i have question when i done teleporting he send message in NPC channel [MESSAGE_GREET] = "Greetings, |PLAYERNAME|.",
how can i delete this this part because he send this message really slowly like only per 4seconds so i want to avoid sending this message and attach new message when i type "hi" he says "NPCName:hi "PlayerName" and when he teleport me he says "NPCName:lets go" how can i make something like this?
That's what this part was for:
LUA:
<parameters>
        <parameter key="message_greet" value="Adeus!"/>
</parameters>
Make sure it's the only message_greet in the xml file.
 
That's what this part was for:
LUA:
<parameters>
        <parameter key="message_greet" value="Adeus!"/>
</parameters>
Make sure it's the only message_greet in the xml file.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Yama" script="yama.lua" walkinterval="0">
<health now="100" max="100" />
<parameter key="message_greet" value="Adeus!"/>
<look type="464"/>
</npc>
It's still send message really slow you talk with him you get teleported and you get message only per 4-5sec
 
Code:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Yama" script="yama.lua" walkinterval="0">
<health now="100" max="100" />
<parameter key="message_greet" value="Adeus!"/>
<look type="464"/>
</npc>
It's still send message really slow you talk with him you get teleported and you get message only per 4-5sec
Might be because it's having a hard time reading the xml without the parameters tag. Mine says it right away with this.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Yama" script="yama.lua" walkinterval="0" floorchange="0">
    <health now="100" max="100" />
    <look type="464"/>
    <parameters>
            <parameter key="message_greet" value="Adeus!"/>
    </parameters>
</npc>
 
Might be because it's having a hard time reading the xml without the parameters tag. Mine says it right away with this.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Yama" script="yama.lua" walkinterval="0" floorchange="0">
    <health now="100" max="100" />
    <look type="464"/>
    <parameters>
            <parameter key="message_greet" value="Adeus!"/>
    </parameters>
</npc>
Still npc send message really slow, nothing changed
 
Back
Top