• 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 npc script luagetnpcdistanceto

Lurk

Active Member
Joined
Dec 4, 2017
Messages
336
Reaction score
49
Hi, I'm using tfs 0.4 and I'm getting this console error
1568905767623.png
when people talk to this npc
LUA:
local config = {
    minlevel = 100000, --- level inical para resetar
    price = 0, --- preço inicial para resetar
    newlevel = 15000, --- level após reset
    priceByReset = 0, --- preço acrescentado por reset
    percent = 50, ---- porcentagem da vida/mana que você terá ao resetar (em relação à sua antiga vida total)
    maxresets = 300,
    newcap = 200000, -- NOVA CAP
    levelbyreset = 15000 --- quanto de level vai precisar a mais no próximo reset
}
--- end config

function getResets(cid)
resets = getPlayerStorageValue(cid,378378)
if getPlayerStorageValue(cid,378378) < 0 then
resets = 0
end
return resets
end

local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}

function onCreatureAppear(cid)
npcHandler:onCreatureAppear(cid)
end
function onCreatureDisappear(cid)
npcHandler:onCreatureDisappear(cid)
setGlobalStorageValue (197823, 0)
end
function onCreatureSay(cid, type, msg)
npcHandler:onCreatureSay(cid, type, msg)
local porra = npcHandler:onCreatureSay(msg)
if porra == bye then
setGlobalStorageValue (197823, 0)
end
end
function onThink()            
npcHandler:onThink()            
end

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

    function addReset(cid)
        if(npcHandler:isFocused(cid)) then
            npcHandler:releaseFocus(cid)
        end
   
        talkState[talkUser] = 0
        resets = getPlayerStorageValue(cid,378378)
   
        if (getPlayerStorageValue(cid,378378)<1) then
            setPlayerStorageValue(cid,378378,1)
        else
            setPlayerStorageValue(cid,378378,resets+1)
        end
   
        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)))
        local hp = getCreatureMaxHealth(cid)
        local resethp = hp*(config.percent/100)
        setCreatureMaxHealth(cid, resethp)
        local differencehp = (hp - resethp)
        doCreatureAddHealth(cid, -differencehp)
        local mana = getCreatureMaxMana(cid)
        local resetmana = mana*(config.percent/100)
        setCreatureMaxMana(cid, resetmana)
        local differencemana = (mana - resetmana)
        doCreatureAddMana(cid, -differencemana)
        doRemoveCreature(cid)  
        --db.executeQuery("UPDATE `players` SET `description` = ' [Reset: "..description.."]' WHERE `players`.`id`= ".. playerid .."")
        db.executeQuery("UPDATE `players` SET `level`="..config.newlevel..",`experience`= 0 WHERE `players`.`id`= ".. playerid .."")
        db.query("UPDATE `players` SET `cap`="..config.newcap.." WHERE `players`.`id`= ".. playerid .."")
        return true
    end

    local newPrice = config.price + (getResets(cid) * config.priceByReset)
    local newminlevel = config.minlevel + (getResets(cid) * config.levelbyreset)

    if msgcontains(msg, 'reset') then
        if getResets(cid) < config.maxresets and getGlobalStorageValue(197823) < 1 then
            setGlobalStorageValue (197823, 1)
            selfSay('You want to reset your character? It will cost '..newPrice..' gp\'s!', cid)
            talkState[talkUser] = 1
        else
            selfSay('I am either talking to someone else at the moment or You have already reached the maximum reset level.', cid)
        end
   
    elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
        if getPlayerMoney(cid) < newPrice then
            selfSay('Its necessary to have at least '..newPrice..' gp\'s for reseting!', cid)
            setGlobalStorageValue (197823, 0)
        elseif getPlayerLevel(cid) < newminlevel or getPlayerStorageValue(cid,378378) > 41 and getPlayerLevel(cid) < 715000 then
            selfSay('The minimum level for reseting is '..newminlevel..'!', cid)
            setGlobalStorageValue (197823, 0)
        else
            doPlayerRemoveMoney(cid,newPrice)
            playerid = getPlayerGUID(cid)
            addEvent(function()
                if isPlayer(cid) then
                    addReset(cid)
                end
            end, 3000)
            local number = getPlayerStorageValue(cid,378378)+1
            local msg ="---[Reset: "..number.."]-- You have reseted!  You'll be disconnected in 3 seconds."
            doPlayerPopupFYI(cid, msg)
            talkState[talkUser] = 0
            npcHandler:releaseFocus(cid)
            setGlobalStorageValue (197823, 0)
        end
        talkState[talkUser] = 0
    elseif(msgcontains(msg, 'no')) and isInArray({1}, talkState[talkUser]) == TRUE then
        talkState[talkUser] = 0
        npcHandler:releaseFocus(cid)
        setGlobalStorageValue (197823, 0)
        selfSay('Ok.', cid)
    elseif msgcontains(msg, 'quantity') then
        selfSay('You have a total of '..getPlayerStorageValue(cid,378378)..' reset(s).', cid)
        talkState[talkUser] = 0
    elseif msgcontains(msg, 'bye') then
        talkState[talkUser] = 0
        setGlobalStorageValue (197823, 0)
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

this is probably something in my npchandler right? if anyone could help.. here's my npchandler.lua -- Advanced NPC System (Created by Jiddo), -- Modified by TheForgottenServer Te - Pastebin.com (https://pastebin.com/260RnG6D)
edit: also, I know this script was originally made for an 0.3.6 engine, everything works but I get this annoying console errors

I know the problem is here
LUA:
function onCreatureSay(cid, type, msg)
npcHandler:onCreatureSay(cid, type, msg)
local porra = npcHandler:onCreatureSay(msg)
if porra == bye then
setGlobalStorageValue (197823, 0)
end
end
and I changed it to
LUA:
local porra = msg
but this is not getting the players message since it's not reseting the storage... how can I correctly get the player's message?
 
Last edited:
Solution
Just use your first script and add this to data/npc/lib/npc.lua
LUA:
function getNpcDistanceTo(id)
    errors(false)
    local thing = getThing(id)

    errors(true)
    if(thing.uid == 0) then
        return nil
    end

    local c = getCreaturePosition(id)
    if(not isValidPosition(c)) then
        return nil
    end

    local s = getCreaturePosition(getNpcId())
    if(not isValidPosition(s) or s.z ~= c.z) then
        return nil
    end

    return math.max(math.abs(s.x - c.x), math.abs(s.y - c.y))
end
Just use your first script and add this to data/npc/lib/npc.lua
LUA:
function getNpcDistanceTo(id)
    errors(false)
    local thing = getThing(id)

    errors(true)
    if(thing.uid == 0) then
        return nil
    end

    local c = getCreaturePosition(id)
    if(not isValidPosition(c)) then
        return nil
    end

    local s = getCreaturePosition(getNpcId())
    if(not isValidPosition(s) or s.z ~= c.z) then
        return nil
    end

    return math.max(math.abs(s.x - c.x), math.abs(s.y - c.y))
end
 
Solution
you're comparing the content of variable porra (which you assigned the content from variable msg) to the content of the variable bye (which if it's not a global, it is probably nil as I didn't see you declare it)
I guess you wanted to compare if msg is 'bye', but you are comparing variables instead

LUA:
function onCreatureSay(cid, type, msg)
    npcHandler:onCreatureSay(cid, type, msg)
    if msg == 'bye' then
        setGlobalStorageValue (197823, 0)
    end
end

I don't know if this is your solution or if what M0ustafa said would be better, but please give meaningful names to variables...
 
Back
Top