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

NPC [0.2.0] - Fully working Banker NPC script

Joined
Aug 1, 2009
Messages
118
Solutions
1
Reaction score
21
[0.3.1] - Fully working Banker NPC script

Notice: This script was made for TFS 0.2, for a 0.3 version see my other post
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local Topic, count, transferTo = {}, {}, {}
local balance_storage = 8996

dofile("./config.lua")

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 getCount(s)
	local b, e = s:find("%d+")
	return b and e and tonumber(s:sub(b, e)) or -1
end

local function getPlayerBalance(cid)
	return math.max(0, getPlayerStorageValue(cid, balance_storage))
end

local function depositMoney(cid, n)
	return setPlayerStorageValue(cid, balance_storage, getPlayerBalance(cid) + n)
end

local function playerExists(name)
	local env, con
	if sqlType == "mysql" then
		env = assert(luasql.mysql())
		con = assert(env:connect(mysqlDatabase, mysqlUser, mysqlPass, mysqlHost, mysqlPort))
	else
		env = assert(luasql.sqlite3())
		con = assert(env:connect(sqliteDatabase))
	end
	local cur = assert(con:execute("SELECT `name`, `id` FROM `players` WHERE `name` = '" .. escapeString(name) .. "' LIMIT 1;"))
	local row = cur:fetch({}, "a")
	cur:close()
	con:close()
	env:close()
	return row.name or 0
end

local function transferGold(player, amount)
	local env, con
	if sqlType == 'mysql' then
		env = assert(luasql.mysql())
		con = assert(env:connect(mysqlDatabase, mysqlUser, mysqlPass, mysqlHost, mysqlPort))
	else
		env = assert(luasql.sqlite3())
		con = assert(env:connect(sqliteDatabase))
	end
	assert(con:execute("UPDATE `player_storage` SET `value` = `value` + " .. amount .. " WHERE `player_id` = (SELECT `id` FROM `players` WHERE `name` = '" .. escapeString(player) .. "' LIMIT 1) AND `key` = '" .. balance_storage .. "' LIMIT 1"))
	con:close()
	env:close()
end

function greetCallback(cid)
	Topic[cid], count[cid], transferTo[cid] = 0
	return true
end

function creatureSayCallback(cid, type, msg)
	if not npcHandler:isFocused(cid) then
		return false
	elseif msgcontains(msg, 'balance') then
		npcHandler:say('Your account balance is ' .. getPlayerBalance(cid) .. ' gold.', cid)
		Topic[cid] = 0
	elseif msgcontains(msg, 'deposit') and msgcontains(msg, 'all') then
		local n = getPlayerMoney(cid)
		if n > 0 then
			count[cid] = n
			npcHandler:say('Would you really like to deposit ' .. n .. ' gold?', cid)
			Topic[cid] = 2
		else
			npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
			Topic[cid] = 1
		end
	elseif msgcontains(msg, 'deposit') then
		local n = getCount(msg)
		if n == 0 then
			npcHandler:say('You are joking, aren\'t you??', cid)
			Topic[cid] = 0
		elseif n ~= -1 then
			if getPlayerMoney(cid) >= n then
				count[cid] = n
				npcHandler:say('Would you really like to deposit ' .. n .. ' gold?', cid)
				Topic[cid] = 2
			else
				npcHandler:say('You do not have enough gold.', cid)
				Topic[cid] = 0
			end
		else
			npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
			Topic[cid] = 1
		end
	elseif Topic[cid] == 1 then
		local n = getCount(msg)
		if n == -1 then
			npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
		else
			if getPlayerMoney(cid) >= n then
				count[cid] = n
				npcHandler:say('Would you really like to deposit ' .. n .. ' gold?', cid)
				Topic[cid] = 2
			else
				npcHandler:say('You do not have enough gold.', cid)
				Topic[cid] = 0
			end
		end
	elseif msgcontains(msg, 'yes') and Topic[cid] == 2 then
		if doPlayerRemoveMoney(cid, count[cid]) == TRUE then
			depositMoney(cid, count[cid])
			npcHandler:say('Alright, we have added the amount of ' .. count[cid] .. ' gold to your balance. You can withdraw your money anytime you want to.', cid)
		else
			npcHandler:say('I am inconsolable, but it seems you have lost your gold. I hope you get it back.', cid)
		end
		Topic[cid] = 0
	elseif msgcontains(msg, 'no') and Topic[cid] == 2 then
		npcHandler:say('As you wish. Is there something else I can do for you?', cid)
		Topic[cid] = 0
	elseif msgcontains(msg, 'withdraw') then
		local n = getCount(msg)
		if n == 0 then
			npcHandler:say('Sure, you want nothing you get nothing!', cid)
			Topic[cid] = 0
		elseif n ~= -1 then
			if getPlayerBalance(cid) >= n then
				count[cid] = n
				npcHandler:say('Are you sure you wish to withdraw ' .. n .. ' gold from your bank account?', cid)
				Topic[cid] = 4
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = 0
			end
		else
			npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
			Topic[cid] = 3
		end
	elseif Topic[cid] == 3 then
		local n = getCount(msg)
		if n == -1 then
			npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
		else
			if getPlayerBalance(cid) >= n then
				count[cid] = n
				npcHandler:say('Are you sure you wish to withdraw ' .. n .. ' gold from your bank account?', cid)
				Topic[cid] = 4
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = 0
			end
		end
	elseif msgcontains(msg, 'yes') and Topic[cid] == 4 then
		if getPlayerBalance(cid) >= count[cid] then
			doPlayerAddMoney(cid, count[cid])
			depositMoney(cid, - count[cid])
			npcHandler:say('Here you are, ' .. count[cid] .. ' gold. Please let me know if there is something else I can do for you.', cid)
		else
			npcHandler:say('There is not enough gold on your account.', cid)
		end
		Topic[cid] = 0
	elseif msgcontains(msg, 'no') and Topic[cid] == 4 then
		npcHandler:say('The customer is king! Come back anytime you want to if you wish to withdraw your money.', cid)
		Topic[cid] = 0
	elseif msgcontains(msg, 'transfer') then
		local n = getCount(msg)
		if n == 0 then
			npcHandler:say('Please think about it. Okay?', cid)
			Topic[cid] = 0
		elseif n ~= -1 then
			count[cid] = n
			if getPlayerBalance(cid) >= count[cid] then
				if msg:find(' to(%s*)') then
					local b,e = msg:find(' to(%s*)')
					transferTo[cid] = msg:sub(e+1)
					local tmp = getPlayerByName(transferTo[cid])
					if tmp then
						npcHandler:say('Would you really like to transfer ' .. count[cid] .. ' gold to ' .. getCreatureName(tmp) .. '?', cid)
						Topic[cid] = 7
					elseif playerExists(transferTo[cid]):lower() == transferTo[cid]:lower() then
						npcHandler:say('Would you really like to transfer ' .. count[cid] .. ' gold to ' .. playerExists(transferTo[cid]) .. '?', cid)
						Topic[cid] = 7
					else
						npcHandler:say('This player does not exist.', cid)
						Topic[cid] = 0
					end
				else
					npcHandler:say('Who would you like to transfer ' .. transferTo[cid] .. ' gold to?', cid)
					Topic[cid] = 6
				end
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = 0
			end
		else
			npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
			Topic[cid] = 5
		end
	elseif Topic[cid] == 5 then
		local n = getCount(msg)
		if n == -1 then
			npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
		elseif n == 0 then
			npcHandler:say('Please think about it. Okay?', cid)
			Topic[cid] = 0
		else
			count[cid] = n
			if getPlayerBalance(cid) >= count[cid] then
				npcHandler:say('Who would you like to transfer ' .. count[cid] .. ' gold to?', cid)
				Topic[cid] = 6
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = 0
			end
		end
	elseif Topic[cid] == 6 then
		local tmp = getPlayerByName(msg)
		if tmp == TRUE then
			if getPlayerBalance(cid) >= count[cid] then
				transferTo[cid] = msg
				npcHandler:say('Would you really like to transfer ' .. count[cid] .. ' gold to ' .. getCreatureName(tmp) .. '?', cid)
				Topic[cid] = 7
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = 0
			end
		elseif playerExists(msg):lower() == msg:lower() then
			if getPlayerBalance(cid) >= count[cid] then
				transferTo[cid] = msg
				npcHandler:say('Would you really like to transfer ' .. count[cid] .. ' gold to ' .. playerExists(msg) .. '?', cid)
				Topic[cid] = 7
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = 0
			end
		else
			npcHandler:say('This player does not exist.', cid)
			Topic[cid] = 0
		end
	elseif Topic[cid] == 7 and msgcontains(msg, 'yes') then
		if getPlayerBalance(cid) >= count[cid] then
			local tmp, state = getPlayerByName(transferTo[cid]), 0
			if not tmp then
				tmp = playerExists(transferTo[cid])
				if tmp:lower() == transferTo[cid]:lower() then
					transferGold(transferTo[cid], count[cid])
					state = 2
				end
			else
				doPlayerAddBalance(tmp, count[cid])
				state = 1
			end
			if state ~= 0 then
				depositMoney(cid, - count[cid])
				npcHandler:say('Very well. You have transferred ' .. count[cid] .. ' gold to ' .. state == 1 and getCreatureName(tmp) or tmp .. '.', cid)
			else
				npcHandler:say('This player does not exist.', cid)
			end
		else
			npcHandler:say('There is not enough gold on your account.', cid)
		end
		Topic[cid] = 0
	elseif Topic[cid] == 7 and msgcontains(msg, 'no') then
		npcHandler:say('Alright, is there something else I can do for you?', cid)
		Topic[cid] = 0
	elseif msgcontains(msg, 'change gold') then
		npcHandler:say('How many platinum coins would you like to get?', cid)
		Topic[cid] = 8
	elseif Topic[cid] == 8 then
		local n = getCount(msg)
		if n < 1 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = 0
		else
			count[cid] = n
			npcHandler:say('So you would like me to change ' .. n * 100 .. ' of your gold coins into ' .. count[cid] .. ' platinum coins?', cid)
			Topic[cid] = 9
		end
	elseif Topic[cid] == 9 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, ITEM_GOLD_COIN, count[cid] * 100) == TRUE then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, ITEM_PLATINUM_COIN, count[cid])
			else
				npcHandler:say('Sorry, you do not have enough gold coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = 0
	elseif msgcontains(msg, 'change platinum') then
		npcHandler:say('Would you like to change your platinum coins into gold or crystal?', cid)
		Topic[cid] = 10
	elseif Topic[cid] == 10 then
		if msgcontains(msg, 'gold') then
			npcHandler:say('How many platinum coins would you like to change into gold?', cid)
			Topic[cid] = 11
		elseif msgcontains(msg, 'crystal') then
			npcHandler:say('How many crystal coins would you like to get?', cid)
			Topic[cid] = 13
		else
			npcHandler:say('Well, can I help you with something else?', cid)
			Topic[cid] = 0
		end
	elseif Topic[cid] == 11 then
		local n = getCount(msg)
		if n < 1 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = 0
		else
			count[cid] = n
			npcHandler:say('So you would like me to change ' .. n .. ' of your platinum coins into ' .. n * 100 .. ' gold coins for you?', cid)
			Topic[cid] = 12
		end
	elseif Topic[cid] == 12 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, ITEM_PLATINUM_COIN, n) == TRUE then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, ITEM_GOLD_COIN, count[cid] * 100)
			else
				npcHandler:say('Sorry, you do not have enough platinum coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = 0
	elseif Topic[cid] == 13 then
		local n = getCount(msg)
		if n < 1 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = 0
		else
			count[cid] = n
			npcHandler:say('So you would like me to change ' .. n * 100 .. ' of your platinum coins into ' .. n .. ' crystal coins for you?', cid)
			Topic[cid] = 14
		end
	elseif Topic[cid] == 14 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, ITEM_PLATINUM_COIN, count[cid] * 100) == TRUE then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, ITEM_CRYSTAL_COIN, count[cid])
			else
				npcHandler:say('Sorry, you do not have enough platinum coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = 0
	elseif msgcontains(msg, 'change crystal') then
		npcHandler:say('How many crystal coins would you like to change into platinum?', cid)
		Topic[cid] = 15
	elseif Topic[cid] == 15 then
		local n = getCount(msg)
		if n < 1 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = 0
		else
			count[cid] = n
			npcHandler:say('So you would like me to change ' .. n .. ' of your crystal coins into ' .. n * 100 .. ' platinum coins for you?', cid)
			Topic[cid] = 16
		end
	elseif Topic[cid] == 16 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, ITEM_CRYSTAL_COIN, count[cid]) == TRUE then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, ITEM_PLATINUM_COIN, count[cid] * 100)
			else
				npcHandler:say('Sorry, you do not have enough crystal coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = 0
	elseif msgcontains(msg, 'change') then
		npcHandler:say('There are three different coin types in Tibia: 100 gold coins equal 1 platinum coin, 100 platinum coins equal 1 crystal coin. So if you\'d like to change 100 gold into 1 platinum, simply say \'{change gold}\' and then \'1 platinum\'.', cid)
		Topic[cid] = 0
	elseif msgcontains(msg, 'bank') then
		npcHandler:say('We can change money for you. You can also access your bank account.', cid)
		Topic[cid] = 0
	end
	return TRUE
end

npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Release history:
Code:
[B][COLOR="Blue"]0.3.1[/COLOR] [COLOR="Red"]-[/COLOR] [COLOR="green"]September/29/2010 1:33 GMT+1[/COLOR][/B]
* More small improvements

[B][COLOR="Blue"]0.3[/COLOR] [COLOR="Red"]-[/COLOR] [COLOR="green"]December/3/2009 4:33 GMT+1[/COLOR][/B]
* Shortened the script

[B][COLOR="Blue"]0.2[/COLOR] [COLOR="Red"]-[/COLOR] [COLOR="green"]August/25/2009 22:03 GMT+2[/COLOR][/B]
* Added functions for [B]changing gold/platinum/crystal coins[/B]
* Added missing [B]function 'getCount(string)'[/B]

[B][COLOR="Blue"]0.1[/COLOR] [COLOR="red"]-[/COLOR] [COLOR="green"]August/25/2009 16:51 GMT+2[/COLOR][/B]
* Initial release
Supported bank functions:
  • balance
  • deposit all
  • deposit
  • withdraw
  • transfer (amount (, to <name))

Report any bugs (ex. duplicating money) in this thread!
 
Last edited:
Nice, I made an NPC script similar to this once...
when you deposit, it puts money in, but leaves in your bp :eek:
People had unlimited money ....

Back when 8.0 was popular.
 
This is what I get when I say stuff like deposit, withdraw, transfer...

PHP:
[25/08/2009 21:03:47] Lua Script Error: [Npc interface] 
[25/08/2009 21:03:47] data/npc/scripts/banker.lua:onCreatureSay

[25/08/2009 21:03:47] data/npc/scripts/banker.lua:83: attempt to call global 'getCount' (a nil value)
[25/08/2009 21:03:47] stack traceback:
[25/08/2009 21:03:47] 	data/npc/scripts/banker.lua:83: in function 'callback'
[25/08/2009 21:03:47] 	data/npc/lib/npcsystem/npchandler.lua:383: in function 'onCreatureSay'
[25/08/2009 21:03:47] 	data/npc/scripts/banker.lua:13: in function <data/npc/scripts/banker.lua:13>

I'm using TFS 0.3.5

Could you fix it?
 
This is what I get when I say stuff like deposit, withdraw, transfer...

PHP:
[25/08/2009 21:03:47] Lua Script Error: [Npc interface] 
[25/08/2009 21:03:47] data/npc/scripts/banker.lua:onCreatureSay

[25/08/2009 21:03:47] data/npc/scripts/banker.lua:83: attempt to call global 'getCount' (a nil value)
[25/08/2009 21:03:47] stack traceback:
[25/08/2009 21:03:47] 	data/npc/scripts/banker.lua:83: in function 'callback'
[25/08/2009 21:03:47] 	data/npc/lib/npcsystem/npchandler.lua:383: in function 'onCreatureSay'
[25/08/2009 21:03:47] 	data/npc/scripts/banker.lua:13: in function <data/npc/scripts/banker.lua:13>

I'm using TFS 0.3.5

Could you fix it?
Oh, sorry. I didn't include that function because I have it in my global.lua
I'll update the main post as soon as possible.

Edit: Updated
 
Last edited:
Tryed it with tfs 0.3.1 but when i say deposit money he says i dont got the money :S
 
Any console errors? :/

No, that's what i am feeling odd.. Tryed another script, (not this one) and it was the same problem there.. I have checked the ids for the coins and everything seems to be in place.. :/

Please help, rep ofc!
 
I'm having no console errors it seems to work fine to withdraw/deposit/balance. However I cant seem to be able to transfer. I have enough gold lol I even tried 1000 gold and didnt work

Code:
There is not enough gold on your account.
:blink:

So at thread title bit fail...

Using tfs 0.3.6pl1 by the way.
 
anyone knows how to fix so ppl can transfer their money into another character/acc?

Transfer with this script doesnt work :confused:
 
For 0.3.6 or higher:
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local Topic, count, transfer = {}, {}, {}

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 getCount(s)
	local b, e = s:find('%d+')
	return b and e and math.min(4294967295, tonumber(s:sub(b, e))) or -1
end

local function findPlayer(name)
	local q = db.getResult('SELECT name FROM players WHERE name=' .. db.escapeString(name) .. ' LIMIT 1'), nil
	if q:getID() == -1 then
		return
	end
	local r = q:getDataString('name')
	q:free()
	return r
end

function greet(cid)
	Topic[cid], count[cid], transfer[cid] = nil, nil, nil
	return true
end

function creatureSayCallback(cid, type, msg)
	if not npcHandler:isFocused(cid) then
		return false
	elseif msgcontains(msg, 'balance') then
		npcHandler:say('Your account balance is ' .. getPlayerBalance(cid) .. ' gold.', cid)
		Topic[cid] = nil
	elseif msgcontains(msg, 'deposit') and msgcontains(msg, 'all') then
		if getPlayerMoney(cid) == 0 then
			npcHandler:say('You don\'t have any gold with you.', cid)
			Topic[cid] = nil
		else
			count[cid] = getPlayerMoney(cid)
			npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
			Topic[cid] = 2
		end
	elseif msgcontains(msg, 'deposit') then
		if getCount(msg) == 0 then
			npcHandler:say('You are joking, aren\'t you??', cid)
			Topic[cid] = nil
		elseif getCount(msg) ~= -1 then
			if getPlayerMoney(cid) >= getCount(msg) then
				count[cid] = getCount(msg)
				npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
				Topic[cid] = 2
			else
				npcHandler:say('You do not have enough gold.', cid)
				Topic[cid] = nil
			end
		elseif getPlayerMoney(cid) == 0 then
			npcHandler:say('You don\'t have any gold with you.', cid)
			Topic[cid] = nil
		else
			npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
			Topic[cid] = 1
		end
	elseif Topic[cid] == 1 then
		if getCount(msg) == -1 then
			npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
			Topic[cid] = 1
		elseif getPlayerMoney(cid) >= getCount(msg) then
			count[cid] = getCount(msg)
			npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
			Topic[cid] = 2
		else
			npcHandler:say('You do not have enough gold.', cid)
			Topic[cid] = nil
		end
	elseif msgcontains(msg, 'yes') and Topic[cid] == 2 then
		if doPlayerRemoveMoney(cid, count[cid]) then
			doPlayerSetBalance(cid, getPlayerBalance(cid) + count[cid])
			npcHandler:say('Alright, we have added the amount of ' .. count[cid] .. ' gold to your balance. You can withdraw your money anytime you want to.', cid)
		else
			npcHandler:say('I am inconsolable, but it seems you have lost your gold. I hope you get it back.', cid)
		end
		Topic[cid] = nil
	elseif msgcontains(msg, 'no') and Topic[cid] == 2 then
		npcHandler:say('As you wish. Is there something else I can do for you?', cid)
		Topic[cid] = nil
	elseif msgcontains(msg, 'withdraw') then
		if getCount(msg) == 0 then
			npcHandler:say('Sure, you want nothing you get nothing!', cid)
			Topic[cid] = nil
		elseif getCount(msg) ~= -1 then
			if getPlayerBalance(cid) >= getCount(msg) then
				count[cid] = getCount(msg)
				npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
				Topic[cid] = 4
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = nil
			end
		elseif getPlayerBalance(cid) == 0 then
			npcHandler:say('You don\'t have any money on your bank account.', cid)
			Topic[cid] = nil
		else
			npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
			Topic[cid] = 3
		end
	elseif Topic[cid] == 3 then
		if getCount(msg) == -1 then
			npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
			Topic[cid] = 3
		elseif getPlayerBalance(cid) >= getCount(msg) then
			count[cid] = getCount(msg)
			npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
			Topic[cid] = 4
		else
			npcHandler:say('There is not enough gold on your account.', cid)
			Topic[cid] = nil
		end
	elseif msgcontains(msg, 'yes') and Topic[cid] == 4 then
		if getPlayerBalance(cid) >= count[cid] then
			doPlayerAddMoney(cid, count[cid])
			doPlayerSetBalance(cid, getPlayerBalance(cid) - count[cid])
			npcHandler:say('Here you are, ' .. count[cid] .. ' gold. Please let me know if there is something else I can do for you.', cid)
		else
			npcHandler:say('There is not enough gold on your account.', cid)
		end
		Topic[cid] = nil
	elseif msgcontains(msg, 'no') and Topic[cid] == 4 then
		npcHandler:say('The customer is king! Come back anytime you want to if you wish to withdraw your money.', cid)
		Topic[cid] = nil
	elseif msgcontains(msg, 'transfer') then
		if getCount(msg) == 0 then
			npcHandler:say('Please think about it. Okay?', cid)
			Topic[cid] = nil
		elseif getCount(msg) ~= -1 then
			count[cid] = getCount(msg)
			if getPlayerBalance(cid) >= count[cid] then
				npcHandler:say('Who would you like to transfer ' .. count[cid] .. ' gold to?', cid)
				Topic[cid] = 6
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = nil
			end
		else
			npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
			Topic[cid] = 5
		end
	elseif Topic[cid] == 5 then
		if getCount(msg) == -1 then
			npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
			Topic[cid] = 5
		else
			count[cid] = getCount(msg)
			if getPlayerBalance(cid) >= count[cid] then
				npcHandler:say('Who would you like to transfer ' .. count[cid] .. ' gold to?', cid)
				Topic[cid] = 6
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = nil
			end
		end
	elseif Topic[cid] == 6 then
		local v = getPlayerByName(msg)
		if getPlayerBalance(cid) >= count[cid] then
			if v then
				transfer[cid] = msg
				npcHandler:say('Would you really like to transfer ' .. count[cid] .. ' gold to ' .. getPlayerName(v) .. '?', cid)
				Topic[cid] = 7
			elseif findPlayer(msg):lower() == msg:lower() then
				transfer[cid] = msg
				npcHandler:say('Would you really like to transfer ' .. count[cid] .. ' gold to ' .. findPlayer(msg) .. '?', cid)
				Topic[cid] = 7
			else
				npcHandler:say('This player does not exist.', cid)
				Topic[cid] = nil
			end
		else
			npcHandler:say('There is not enough gold on your account.', cid)
			Topic[cid] = nil
		end
	elseif Topic[cid] == 7 and msgcontains(msg, 'yes') then
		if getPlayerBalance(cid) >= count[cid] then
			local v = getPlayerByName(transfer[cid])
			if v then
				doPlayerSetBalance(cid, getPlayerBalance(cid) - count[cid])
				doPlayerSetBalance(v, getPlayerBalance(v) + count[cid])
				npcHandler:say('Very well. You have transferred ' .. count[cid] .. ' gold to ' .. getPlayerName(v) .. '.', cid)
			elseif findPlayer(transfer[cid]):lower() == transfer[cid]:lower() then
				doPlayerSetBalance(cid, getPlayerBalance(cid) - count[cid])
				db.executeQuery('UPDATE players SET balance=balance+' .. count[cid] .. ' WHERE name=' .. db.escapeString(transfer[cid]) .. ' LIMIT 1')
				npcHandler:say('Very well. You have transferred ' .. count[cid] .. ' gold to ' .. findPlayer(transfer[cid]) .. '.', cid)
			else
				npcHandler:say('This player does not exist.', cid)
			end
		else
			npcHandler:say('There is not enough gold on your account.', cid)
		end
		Topic[cid] = nil
	elseif Topic[cid] == 7 and msgcontains(msg, 'no') then
		npcHandler:say('Alright, is there something else I can do for you?', cid)
		Topic[cid] = nil
	elseif msgcontains(msg, 'change gold') then
		npcHandler:say('How many platinum coins would you like to get?', cid)
		Topic[cid] = 8
	elseif Topic[cid] == 8 then
		if getCount(msg) < 1 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = nil
		else
			count[cid] = math.min(500, getCount(msg))
			npcHandler:say('So you would like me to change ' .. count[cid] * 100 .. ' of your gold coins into ' .. count[cid] .. ' platinum coins?', cid)
			Topic[cid] = 9
		end
	elseif Topic[cid] == 9 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, 2148, count[cid] * 100) then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, 2152, count[cid])
			else
				npcHandler:say('Sorry, you do not have enough gold coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = nil
	elseif msgcontains(msg, 'change platinum') then
		npcHandler:say('Would you like to change your platinum coins into gold or crystal?', cid)
		Topic[cid] = 10
	elseif Topic[cid] == 10 then
		if msgcontains(msg, 'gold') then
			npcHandler:say('How many platinum coins would you like to change into gold?', cid)
			Topic[cid] = 11
		elseif msgcontains(msg, 'crystal') then
			npcHandler:say('How many crystal coins would you like to get?', cid)
			Topic[cid] = 13
		else
			npcHandler:say('Well, can I help you with something else?', cid)
			Topic[cid] = nil
		end
	elseif Topic[cid] == 11 then
		if getCount(msg) < 1 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = nil
		else
			count[cid] = math.min(500, getCount(msg))
			npcHandler:say('So you would like me to change ' .. count[cid] .. ' of your platinum coins into ' .. count[cid] * 100 .. ' gold coins for you?', cid)
			Topic[cid] = 12
		end
	elseif Topic[cid] == 12 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, 2152, count[cid]) then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, 2148, count[cid] * 100)
			else
				npcHandler:say('Sorry, you do not have enough platinum coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = nil
	elseif Topic[cid] == 13 then
		if getCount(msg) < 1 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = nil
		else
			count[cid] = math.min(500, getCount(msg))
			npcHandler:say('So you would like me to change ' .. count[cid] * 100 .. ' of your platinum coins into ' .. count[cid] .. ' crystal coins for you?', cid)
			Topic[cid] = 14
		end
	elseif Topic[cid] == 14 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, 2152, count[cid] * 100) then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, 2160, count[cid])
			else
				npcHandler:say('Sorry, you do not have enough platinum coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = nil
	elseif msgcontains(msg, 'change crystal') then
		npcHandler:say('How many crystal coins would you like to change into platinum?', cid)
		Topic[cid] = 15
	elseif Topic[cid] == 15 then
		if getCount(msg) == -1 or getCount(msg) == 0 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = nil
		else
			count[cid] = math.min(500, getCount(msg))
			npcHandler:say('So you would like me to change ' .. count[cid] .. ' of your crystal coins into ' .. count[cid] * 100 .. ' platinum coins for you?', cid)
			Topic[cid] = 16
		end
	elseif Topic[cid] == 16 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, 2160, count[cid]) then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, 2152, count[cid] * 100)
			else
				npcHandler:say('Sorry, you do not have enough crystal coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = nil
	elseif msgcontains(msg, 'change') then
		npcHandler:say('There are three different coin types in Tibia: 100 gold coins equal 1 platinum coin, 100 platinum coins equal 1 crystal coin. So if you\'d like to change 100 gold into 1 platinum, simply say \'{change gold}\' and then \'1 platinum\'.', cid)
		Topic[cid] = nil
	elseif msgcontains(msg, 'bank') then
		npcHandler:say('We can change money for you. You can also access your bank account.', cid)
		Topic[cid] = nil
	end
	return true
end

npcHandler:setCallback(CALLBACK_GREET, greet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Last edited:
For 0.3.x:
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local Topic, count, transferTo_name = {}, {}, {}

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 getCount(string)
	local b, e = string:find('%d+')
	return b and e and tonumber(string:sub(b, e)) or -1
end

local function playerExists(name)
	local v, ret = db.getResult("SELECT `name` FROM `players` WHERE `name` = " .. db.escapeString(name) .. ";"), nil
	if v:getID() ~= -1 then
		ret = v:getDataString('name')
	end
	v:free()
	return ret
end

function greetCallback(cid)
	Topic[cid], count[cid], transferTo_name[cid] = 0, 0,0
	return true
end

function creatureSayCallback(cid, type, msg)
	if not npcHandler:isFocused(cid) then
		return false
	elseif msgcontains(msg, 'balance') then
		npcHandler:say('Your account balance is ' .. getPlayerBalance(cid) .. ' gold.', cid)
		Topic[cid] = 0
	elseif msgcontains(msg, 'deposit') and msgcontains(msg, 'all') then
		if getPlayerMoney(cid) > 0 then
			count[cid] = getPlayerMoney(cid)
			npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
			Topic[cid] = 2
		else
			npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
			Topic[cid] = 1
		end
	elseif msgcontains(msg, 'deposit') then
		if getCount(msg) == 0 then
			npcHandler:say('You are joking, aren\'t you??', cid)
			Topic[cid] = 0
		elseif getCount(msg) ~= -1 then
			if getPlayerMoney(cid) >= getCount(msg) then
				count[cid] = getCount(msg)
				npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
				Topic[cid] = 2
			else
				npcHandler:say('You do not have enough gold.', cid)
				Topic[cid] = 0
			end
		else
			npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
			Topic[cid] = 1
		end
	elseif Topic[cid] == 1 then
		if getCount(msg) == -1 then
			npcHandler:say('Please tell me how much gold it is you would like to deposit.', cid)
			Topic[cid] = 1
		else
			if getPlayerMoney(cid) >= getCount(msg) then
				count[cid] = getCount(msg)
				npcHandler:say('Would you really like to deposit ' .. count[cid] .. ' gold?', cid)
				Topic[cid] = 2
			else
				npcHandler:say('You do not have enough gold.', cid)
				Topic[cid] = 0
			end
		end
	elseif msgcontains(msg, 'yes') and Topic[cid] == 2 then
		if doPlayerRemoveMoney(cid, count[cid]) then
			doPlayerSetBalance(cid, getPlayerBalance(cid) + count[cid])
			npcHandler:say('Alright, we have added the amount of ' .. count[cid] .. ' gold to your balance. You can withdraw your money anytime you want to.', cid)
		else
			npcHandler:say('I am inconsolable, but it seems you have lost your gold. I hope you get it back.', cid)
		end
		Topic[cid] = 0
	elseif msgcontains(msg, 'no') and Topic[cid] == 2 then
		npcHandler:say('As you wish. Is there something else I can do for you?', cid)
		Topic[cid] = 0
	elseif msgcontains(msg, 'withdraw') then
		if getCount(msg) == 0 then
			npcHandler:say('Sure, you want nothing you get nothing!', cid)
			Topic[cid] = 0
		elseif getCount(msg) ~= -1 then
			if getPlayerBalance(cid) >= getCount(msg) then
				count[cid] = getCount(msg)
				npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
				Topic[cid] = 4
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = 0
			end
		else
			npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
			Topic[cid] = 3
		end
	elseif Topic[cid] == 3 then
		if getCount(msg) == -1 then
			npcHandler:say('Please tell me how much gold you would like to withdraw.', cid)
			Topic[cid] = 3
		else
			if getPlayerBalance(cid) >= getCount(msg) then
				count[cid] = getCount(msg)
				npcHandler:say('Are you sure you wish to withdraw ' .. count[cid] .. ' gold from your bank account?', cid)
				Topic[cid] = 4
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = 0
			end
		end
	elseif msgcontains(msg, 'yes') and Topic[cid] == 4 then
		if getPlayerBalance(cid) >= count[cid] then
			doPlayerAddMoney(cid, count[cid])
			doPlayerSetBalance(cid, getPlayerBalance(cid) - count[cid])
			npcHandler:say('Here you are, ' .. count[cid] .. ' gold. Please let me know if there is something else I can do for you.', cid)
		else
			npcHandler:say('There is not enough gold on your account.', cid)
		end
		Topic[cid] = 0
	elseif msgcontains(msg, 'no') and Topic[cid] == 4 then
		npcHandler:say('The customer is king! Come back anytime you want to if you wish to withdraw your money.', cid)
		Topic[cid] = 0
	elseif msgcontains(msg, 'transfer') then
		if getCount(msg) == 0 then
			npcHandler:say('Please think about it. Okay?', cid)
			Topic[cid] = 0
		elseif getCount(msg) ~= -1 then
			count[cid] = getCount(msg)
			if getPlayerBalance(cid) >= count[cid] then
				npcHandler:say('Who would you like to transfer ' .. count[cid] .. ' gold to?', cid)
				Topic[cid] = 6
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = 0
			end
		else
			npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
			Topic[cid] = 5
		end
	elseif Topic[cid] == 5 then
		if getCount(msg) == -1 then
			npcHandler:say('Please tell me the amount of gold you would like to transfer.', cid)
			Topic[cid] = 5
		else
			count[cid] = getCount(msg)
			if getPlayerBalance(cid) >= count[cid] then
				npcHandler:say('Who would you like to transfer ' .. count[cid] .. ' gold to?', cid)
				Topic[cid] = 6
			else
				npcHandler:say('There is not enough gold on your account.', cid)
				Topic[cid] = 0
			end
		end
	elseif Topic[cid] == 6 then
		local v = getPlayerByName(msg)
		if getPlayerBalance(cid) >= count[cid] then
			if v then
				transferTo_name[cid] = msg
				npcHandler:say('Would you really like to transfer ' .. count[cid] .. ' gold to ' .. getPlayerName(v) .. '?', cid)
				Topic[cid] = 7
			elseif playerExists(msg):lower() == msg:lower() then
				transferTo_name[cid] = msg
				npcHandler:say('Would you really like to transfer ' .. count[cid] .. ' gold to ' .. playerExists(msg) .. '?', cid)
				Topic[cid] = 7
			else
				npcHandler:say('This player does not exist.', cid)
				Topic[cid] = 0
			end
		else
			npcHandler:say('There is not enough gold on your account.', cid)
			Topic[cid] = 0
		end
	elseif Topic[cid] == 7 and msgcontains(msg, 'yes') then
		if getPlayerBalance(cid) >= count[cid] then
			local v = getPlayerByName(transferTo_name[cid])
			if v then
				doPlayerSetBalance(cid, getPlayerBalance(cid) - count[cid])
				doPlayerSetBalance(v, getPlayerBalance(v) + count[cid])
				npcHandler:say('Very well. You have transferred ' .. count[cid] .. ' gold to ' .. getPlayerName(v) .. '.', cid)
			elseif playerExists(transferTo_name[cid]):lower() == transferTo_name[cid]:lower() then
				doPlayerSetBalance(cid, getPlayerBalance(cid) - count[cid])
				db.executeQuery('UPDATE `players` SET `balance` = `balance` + ' .. count[cid] .. ' WHERE `name` = ' .. db.escapeString(transferTo_name[cid]) .. ' LIMIT 1;')
				npcHandler:say('Very well. You have transferred ' .. count[cid] .. ' gold to ' .. playerExists(transferTo_name[cid]) .. '.', cid)
			else
				npcHandler:say('This player does not exist.', cid)
			end
		else
			npcHandler:say('There is not enough gold on your account.', cid)
		end
		Topic[cid] = 0
	elseif Topic[cid] == 7 and msgcontains(msg, 'no') then
		npcHandler:say('Alright, is there something else I can do for you?', cid)
		Topic[cid] = 0
	elseif msgcontains(msg, 'change gold') then
		npcHandler:say('How many platinum coins would you like to get?', cid)
		Topic[cid] = 8
	elseif Topic[cid] == 8 then
		if getCount(msg) < 1 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = 0
		else
			count[cid] = getCount(msg)
			npcHandler:say('So you would like me to change ' .. count[cid] * 100 .. ' of your gold coins into ' .. count[cid] .. ' platinum coins?', cid)
			Topic[cid] = 9
		end
	elseif Topic[cid] == 9 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, 2148, count[cid] * 100) then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, 2152, count[cid])
			else
				npcHandler:say('Sorry, you do not have enough gold coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = 0
	elseif msgcontains(msg, 'change platinum') then
		npcHandler:say('Would you like to change your platinum coins into gold or crystal?', cid)
		Topic[cid] = 10
	elseif Topic[cid] == 10 then
		if msgcontains(msg, 'gold') then
			npcHandler:say('How many platinum coins would you like to change into gold?', cid)
			Topic[cid] = 11
		elseif msgcontains(msg, 'crystal') then
			npcHandler:say('How many crystal coins would you like to get?', cid)
			Topic[cid] = 13
		else
			npcHandler:say('Well, can I help you with something else?', cid)
			Topic[cid] = 0
		end
	elseif Topic[cid] == 11 then
		if getCount(msg) < 1 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = 0
		else
			count[cid] = getCount(msg)
			npcHandler:say('So you would like me to change ' .. count[cid] .. ' of your platinum coins into ' .. count[cid] * 100 .. ' gold coins for you?', cid)
			Topic[cid] = 12
		end
	elseif Topic[cid] == 12 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, 2152, count[cid]) then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, 2148, count[cid] * 100)
			else
				npcHandler:say('Sorry, you do not have enough platinum coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = 0
	elseif Topic[cid] == 13 then
		if getCount(msg) < 1 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = 0
		else
			count[cid] = getCount(msg)
			npcHandler:say('So you would like me to change ' .. count[cid] * 100 .. ' of your platinum coins into ' .. count[cid] .. ' crystal coins for you?', cid)
			Topic[cid] = 14
		end
	elseif Topic[cid] == 14 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, 2152, count[cid] * 100) then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, 2160, count[cid])
			else
				npcHandler:say('Sorry, you do not have enough platinum coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = 0
	elseif msgcontains(msg, 'change crystal') then
		npcHandler:say('How many crystal coins would you like to change into platinum?', cid)
		Topic[cid] = 15
	elseif Topic[cid] == 15 then
		if getCount(msg) == -1 or getCount(msg) == 0 then
			npcHandler:say('Hmm, can I help you with something else?', cid)
			Topic[cid] = 0
		else
			count[cid] = getCount(msg)
			npcHandler:say('So you would like me to change ' .. count[cid] .. ' of your crystal coins into ' .. count[cid] * 100 .. ' platinum coins for you?', cid)
			Topic[cid] = 16
		end
	elseif Topic[cid] == 16 then
		if msgcontains(msg, 'yes') then
			if doPlayerRemoveItem(cid, 2160, count[cid]) then
				npcHandler:say('Here you are.', cid)
				doPlayerAddItem(cid, 2152, count[cid] * 100)
			else
				npcHandler:say('Sorry, you do not have enough crystal coins.', cid)
			end
		else
			npcHandler:say('Well, can I help you with something else?', cid)
		end
		Topic[cid] = 0
	elseif msgcontains(msg, 'change') then
		npcHandler:say('There are three different coin types in Tibia: 100 gold coins equal 1 platinum coin, 100 platinum coins equal 1 crystal coin. So if you\'d like to change 100 gold into 1 platinum, simply say \'{change gold}\' and then \'1 platinum\'.', cid)
		Topic[cid] = 0
	elseif msgcontains(msg, 'bank') then
		npcHandler:say('We can change money for you. You can also access your bank account.', cid)
		Topic[cid] = 0
	end
	return TRUE
end

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

Tryed it on tfs 0.3.1:

[11/05/2010 17:33:10] data/npc/scripts/bank.lua:79: attempt to call global 'doPlayerSetBalance' (a nil value)
[11/05/2010 17:33:10] stack traceback:
[11/05/2010 17:33:10] data/npc/scripts/bank.lua:79: in function 'callback'
[11/05/2010 17:33:10] data/npc/lib/npcsystem/npchandler.lua:374: in function 'onCreatureSay'
[11/05/2010 17:33:10] data/npc/scripts/bank.lua:9: in function <data/npc/scripts/bank.lua:9>

17:33 Admin Johan [9999]: deposit 1000
17:33 Banker: Would you really like to deposit 1000 gold?
17:33 Admin Johan [9999]: yes

nothing happends.
 
Back
Top