• 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 Help with NPC - Bounty Hunter

Allesn

Member
Joined
May 4, 2011
Messages
204
Solutions
1
Reaction score
24
One would update the NPC for it to work in tfs 1.2?

NPC Code:
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
-- OTServ event handling functions start
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
-- OTServ event handling functions end
local talkState = {}
local huntN = {}
local huntP = {}

local function checkName(player)
local sp_id = getPlayerGUIDByName(player)
if sp_id == nil then
return FALSE
end

local id = 0
local result_plr = db.getResult("SELECT * FROM `bounty_hunters` WHERE `sp_id` = "..sp_id.." AND `killed` = 0;")
if(result_plr:getID() ~= -1) then
id = tonumber(result_plr:getDataInt("sp_id"))
result_plr:free()
end

if id ~= sp_id then
return TRUE
end
return FALSE
end

local function huntPlayer(cid,player,cost)
local guid = tonumber(getPlayerGUID(cid))
local player = tonumber(getPlayerGUIDByName(player))
local cost = tonumber(cost)

if guid == nil or player == nil or cost == nil then
return FALSE
end
db.executeQuery("INSERT INTO `bounty_hunters` VALUES (NULL,"..guid..","..player..",0," .. os.time() .. ","..cost..",0,0);")
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "[BOUNTY HUNTERS] Hunt has been added successfuly!")
return TRUE
end

function creatureSayCallback(cid, type, msg)
-- Place all your code in here. Remember that hi, bye and all that stuff is already handled by the npcsystem, so you do not have to take care of that yourself.
if(not(npcHandler:isFocused(cid))) then
return false
end
if talkState[cid] == nil then
talkState[cid] = 0
end
if (msgcontains(msg, 'tak') or msgcontains(msg, 'yes') or msgcontains(msg, 'hunt')) and talkState[cid] == 0 then
selfSay('Oh, you want to kill somebody, don`t you? Tell me, what is his name.',cid)
talkState[cid] = 1
elseif talkState[cid]==1 then
talkState[cid] = 0
if checkName(msg) == TRUE then
huntN[cid] = msg
selfSay('Okay. You can hunt him. Tell me, how much {thousands} {of} {gold} {coins} will you pay to the killer.',cid)
talkState[cid] = 2
else
selfSay('Sorry, this player is already hunted or does not exist. Really sorry!',cid)
end
elseif talkState[cid] == 2 then
talkState[cid] = 0
kwota = tonumber(msg)
if kwota == nil then
selfSay('What? I don`t understand you.', cid)
elseif kwota > 10000000 then
selfSay('Sorry, the maximum amount of gold coins is 10 000 000K.', cid)
elseif kwota < 1 then
selfSay('Sorry, the minimum amount of gold coins is 1 000.', cid)
else
huntP[cid] = kwota
selfSay('Do you want to hunt the player {'..huntN[cid]..'} for {'..huntP[cid]..'K} gold coins?', cid)
talkState[cid] = 3
end
elseif talkState[cid] == 3 then
if msgcontains(msg, 'yes') then
if doPlayerRemoveMoney(cid,huntP[cid]*1000) then

if huntPlayer(cid,huntN[cid],huntP[cid]*1000) == TRUE then
selfSay('You have added the hunt announcement of killing the player {'..huntN[cid]..'} for {'..huntP[cid]..'K} gold coins!', cid)
else
selfSay('Maybe next time.',cid)
end

else
selfSay('Sorry, you do not have enough money. Maybe next time!',cid)
end
else
selfSay('Maybe next time.',cid)
end
talkState[cid]=0
end
-- Place all your code in here. Remember that hi, bye and all that stuff is already handled by the npcsystem, so you do not have to take care of that yourself.
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

Topic with old version:
https://otland.net/threads/bounty-hunters-system-player-hunt-system.27721/
 
Try this:
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
-- OTServ event handling functions start
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
-- OTServ event handling functions end
local talkState = {}
local huntN = {}
local huntP = {}

local function checkName(player)
local sp_id = player:getGUID()
if sp_id == nil then
return false
end

local id = 0
local result_plr = db.storeQuery("SELECT * FROM `bounty_hunters` WHERE `sp_id` = "..sp_id.." AND `killed` = 0;")
local getid = result.getDataInt(result_plr, "sp_id")
if(getid ~= -1) then
id = tonumber(result.getDataInt(result_plr, "sp_id"))
result_plr:free()
end

if id ~= sp_id then
return true
end
return false
end

local function huntPlayer(cid,player,cost)
local guid = tonumber(getPlayerGUID(cid))
local player = tonumber(getPlayerByName(player))
local cost = tonumber(cost)

if guid == nil or player == nil or cost == nil then
return false
end
db.query("INSERT INTO `bounty_hunters` VALUES (NULL,"..guid..","..player..",0," .. os.time() .. ","..cost..",0,0);")
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "[BOUNTY HUNTERS] Hunt has been added successfuly!")
return true
end

function creatureSayCallback(cid, type, msg)
-- Place all your code in here. Remember that hi, bye and all that stuff is already handled by the npcsystem, so you do not have to take care of that yourself.
if(not(npcHandler:isFocused(cid))) then
return false
end
if npcHandler.topic[cid] == nil then
npcHandler.topic[cid] = 0
end
if (msgcontains(msg, 'tak') or msgcontains(msg, 'yes') or msgcontains(msg, 'hunt')) and npcHandler.topic[cid] == 0 then
selfSay('Oh, you want to kill somebody, don`t you? Tell me, what is his name.',cid)
npcHandler.topic[cid] = 1
elseif npcHandler.topic[cid]==1 then
npcHandler.topic[cid] = 0
if checkName(msg) == true then
huntN[cid] = msg
selfSay('Okay. You can hunt him. Tell me, how much {thousands} {of} {gold} {coins} will you pay to the killer.',cid)
npcHandler.topic[cid] = 2
else
selfSay('Sorry, this player is already hunted or does not exist. Really sorry!',cid)
end
elseif npcHandler.topic[cid] == 2 then
npcHandler.topic[cid] = 0
kwota = tonumber(msg)
if kwota == nil then
selfSay('What? I don`t understand you.', cid)
elseif kwota > 10000000 then
selfSay('Sorry, the maximum amount of gold coins is 10 000 000K.', cid)
elseif kwota < 1 then
selfSay('Sorry, the minimum amount of gold coins is 1 000.', cid)
else
huntP[cid] = kwota
selfSay('Do you want to hunt the player {'..huntN[cid]..'} for {'..huntP[cid]..'K} gold coins?', cid)
npcHandler.topic[cid] = 3
end
elseif npcHandler.topic[cid] == 3 then
if msgcontains(msg, 'yes') then
if doPlayerRemoveMoney(cid,huntP[cid]*1000) then

if huntPlayer(cid,huntN[cid],huntP[cid]*1000) == true then
selfSay('You have added the hunt announcement of killing the player {'..huntN[cid]..'} for {'..huntP[cid]..'K} gold coins!', cid)
else
selfSay('Maybe next time.',cid)
end

else
selfSay('Sorry, you do not have enough money. Maybe next time!',cid)
end
else
selfSay('Maybe next time.',cid)
end
npcHandler.topic[cid] = 0
end
-- Place all your code in here. Remember that hi, bye and all that stuff is already handled by the npcsystem, so you do not have to take care of that yourself.
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

Remember that it's a little bit hard convert scripts from TFS 0.3.6 or 0.4 to 1.1 or higher (1.2), so it's possible that there's a little bug.
Anyway, it's easier make your own npc's, so you can learn how it works and you can use your imagination.
 
Error :/
Code:
Lua Script Error: [Npc interface]
data/npc/scripts/bounty.lua:onCreatureSay
data/npc/scripts/bounty.lua:15: attempt to call method 'getGUID' (a nil value)
stack traceback:
        [C]: in function 'getGUID'
        data/npc/scripts/bounty.lua:15: in function 'checkName'
        data/npc/scripts/bounty.lua:60: in function 'callback'
        data/npc/lib/npcsystem/npchandler.lua:411: in function 'onCreatureSay'
        data/npc/scripts/bounty.lua:7: in function <data/npc/scripts/bounty.lua:7>
 
Error :/
Code:
Lua Script Error: [Npc interface]
data/npc/scripts/bounty.lua:onCreatureSay
data/npc/scripts/bounty.lua:15: attempt to call method 'getGUID' (a nil value)
stack traceback:
        [C]: in function 'getGUID'
        data/npc/scripts/bounty.lua:15: in function 'checkName'
        data/npc/scripts/bounty.lua:60: in function 'callback'
        data/npc/lib/npcsystem/npchandler.lua:411: in function 'onCreatureSay'
        data/npc/scripts/bounty.lua:7: in function <data/npc/scripts/bounty.lua:7>
You must be sure that your compat.lua is updated.
 
The file you sent me is already so you said it was to change

EDIT:
This is strange, because I used the talkaction to ban a character (/ban char), and works (the talkaction use player:getGuid())
 
Last edited:
The file you sent me is already so you said it was to change

EDIT:
This is strange, because I used the talkaction to ban a character (/ban char), and works (the talkaction use player:getGuid())
To fix the console error that you showed me before, you must change in line 15:
Code:
player:getGUID()
To:
Code:
player:getGuid()
And that's it.
 

Similar threads

Back
Top