• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

NPC Banker (with Change gold)

Ispiro

New Member
Joined
Oct 1, 2007
Messages
129
Reaction score
2
I had a script released before, it was a perfect change gold NPC, but I've been trying to make a bank NPC and this is the closest i've come so far...

Credits to Ispiro, Jiddo, sapphire and colex.

Code:
-- Change Gold NPC, includes banking functions except transfer
-- Made by Ispiro.
-- Credits to Colex for some of the banker functions
-- Credits to Jiddo for getCount function
-- Credits to Sapphire for some fixes

local focus = 0
local talk_start = 0
local talk_state = 0
-- change gold variables
local difference = 1
local typeToChange = ""
local newType = ""
local multiplier = 1
local count = 0
-- banker
local name = ""
local amountToHandle = 0

function onThingMove(creature, thing, oldpos, oldstackpos)

end


function onCreatureAppear(creature)

end


function onCreatureDisappear(cid, pos)
	if focus == cid then
		selfSay('Good bye then.')
		focus = 0
		talk_state = 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)
	if focus == 0 then
		if (msgcontains(msg, 'hi')) and getDistanceTo(cid) < 4 then
			focus = cid
			talk_start = os.clock()	
			name = getPlayerName(cid)
			selfSay('Welcome ' .. name .. '! Daraman\'s blessings.')	
		end	
	elseif focus == cid then
		talk_start = os.clock()
		if talk_state > 0 then
			if talk_state >= 2 and talk_state <= 6 then
				count = getCountChangeGold(msg)
				if talk_state == 3 then	
					if (msgcontains(msg, 'gold')) then
						talk_state = 5
						selfSay('How much platinums would you like to change?')
					elseif (msgcontains(msg, 'crystal')) then	
						talk_state = 6	
						selfSay('How many crystal would you like to get?')							
					else
						talk_state = 0
						selfSay('Well, can I help you with something else?')
					end		
				elseif count ~= 0 then
					if talk_state == 2 then
						typeToChange = "gold"
						newType = "platinum"
						difference = 100
						multiplier = 1
					
					elseif talk_state == 4 then	
						typeToChange = "crystal"
						newType = "platinum"	
						multiplier = 100	
						difference = 1		
					elseif talk_state == 5 then	
						typeToChange = "platinum"
						newType = "gold"
						multiplier = 100	
						difference = 1		
					elseif talk_state == 6 then	
						typeToChange = "platinum"
						newType = "crystal"	
						multiplier = 1	
						difference = 100							
					end
					if(talk_state <= 6 and talk_state >= 2) then
						selfSay('So you would like me to change '.. count*difference ..' of your '.. typeToChange ..' coins into '.. count*multiplier ..' '.. newType ..' coins?')	
						talk_state = 7
					end	
				end			
			elseif talk_state == 7 then
				if (msgcontains(msg, 'yes')) then
					local haveEnough = changeGold(cid, count, typeToChange, newType, difference, multiplier)
					if haveEnough ~= 0 then
						selfSay('Here you are.')
					else
						selfSay('You don\'t have enough '.. typeToChange ..' coins.')				
					end
				else
					selfSay('Well, can I help you with something else?')
				end	
				talk_state = 0
			elseif talk_state > 7 then
			-- banker functions
				if (talk_state == 8) then -- deposit all
					if(msgcontains(msg, 'yes')) then
						local depmoney = depositAllMoney(name)
						selfSay('You have safely deposited '.. depmoney ..' gold coins into your account.')
					else
						selfSay('Maybe later.')
					end					
					talk_state = 0
				elseif (talk_state == 9) then  -- deposit certain amount	
					amountToHandle = getCountChangeGold(msg)
					if(amountToHandle > 0) then
						selfSay('Do you want to deposit '.. amountToHandle ..' gold coins?')
						talk_state = 12
					else
						selfSay('Maybe later...')
						talk_state = 0
					end
				elseif (talk_state == 12) then  -- deposit certain amount
					if(msgcontains(msg, 'yes')) then
						if(doPlayerRemoveMoney(cid, amountToHandle) == TRUE) then
							local playerMoney = getAmountOfMoney(name)
							changeMoney(name, playerMoney + amountToHandle)
							selfSay('Your money has been deposited safely.')
						else
							selfSay('You do not have that much money.')
						end	
					else
						selfSay('Maybe later...')
					end
					talk_state = 0				
				elseif talk_state == 13 then  -- deposit certain amount	
					amountToHandle = getCountChangeGold(msg)
					if(amountToHandle > 0) then
						selfSay('Would you like to withdraw '.. amountToHandle ..' gold?')
					else
						selfSay('Maybe Later...')
					end	
					talk_state = 14	
				elseif (talk_state == 14) then  -- deposit certain amount	
					if(msgcontains(msg, 'yes')) then
						local playerMoney = getAmountOfMoney(name)
						if(playerMoney >= amountToHandle) then
							changeMoney(name, playerMoney - amountToHandle)
							addMoney(cid, amountToHandle)
							selfSay('Here you go.')
						else
							selfSay('You do not have that much money in your balance.')
						end
					else
						selfSay('Maybe later...')
					end	
					talk_state = 0						
				end	
					
			end		
			
		else
			if (msgcontains(msg, 'hi')) then
				selfSay('I\'m already talking to you!')	
				
			elseif (msgcontains(msg, 'offer')) then
				selfSay('We can change money for you. ')
				
			elseif ((msgcontains(msg, 'bye')) and (focus == cid))then
				selfSay('Daraman\'s blessings. ')	
				focus = 0
				talk_state = 0	
			elseif (msgcontains(msg, 'change gold')) then -- 1
				selfSay('How many platinum coins would you like to get?')
				talk_state = 2
			elseif (msgcontains(msg, 'change platinum')) then -- 2
				selfSay('Do you want to get gold or crystal coins?')
				talk_state = 3
			elseif (msgcontains(msg, 'change crystal')) then
				selfSay('How many crystal coins would you like to change to platinum?')
				talk_state = 4	
			elseif (msgcontains(msg, 'balance')) then
				selfSay('You have '.. getAmountOfMoney(getPlayerName(cid)) ..' gold coins in your balance.')
			elseif (msgcontains(msg, 'deposit all')) then
				selfSay('Would you like to deposit all your money?')	
				talk_state = 8	
			elseif (msgcontains(msg, 'deposit')) then
				amountToHandle = getCountChangeGold(msg)
				if(amountToHandle > 0) then
					talk_state = 12
					selfSay('Do you want to deposit '.. amountToHandle ..' gold coins?')		
				else
					selfSay('How much gold coins do you wish to deposit?')		
					talk_state = 9
				end	
			elseif (msgcontains(msg, 'withdraw')) then
				amountToHandle = getCountChangeGold(msg)
				if(amountToHandle > 0) then
					selfSay('Would you like to withdraw '.. amountToHandle ..' gold?')	
					talk_state = 14
				else
					selfSay('How much do you wish to withdraw?')
					talk_state = 13
				end				
			-- transfer will be added when it's 						
			end
		end
	end	
end


function onCreatureChangeOutfit(creature)

end

function onThink()
	doNpcSetCreatureFocus(focus)
	if(focus ~= 0) then
		if (os.clock() - talk_start) > 40 then
  			selfSay('Next Please...')
			focus = 0
			talk_state = 0
  		elseif getDistanceTo(focus) > 5 then
 			selfSay('Good bye then.')
			focus = 0
			talk_state = 0
 		end
 	end
end

function changeGold(cid, count, typeToChange, newType, difference, multiplier)
-- Made by Ispiro
	local countToChange = 0
	local goldType = 0	
	local newGoldType = 0
	local gpstogive = count*multiplier
	
	if typeToChange == "platinum" then		
		goldType = 2152
	elseif typeToChange == "gold" then
		goldType = 2148
	elseif typeToChange == "crystal" then
		goldType = 2160
	else
		return 0	
	end
	
	countToChange = getPlayerItemCount(cid, goldType)
	if countToChange >= count*difference then
		if newType == "platinum" then
			newGoldType = 2152
		elseif newType == "gold" then
			newGoldType = 2148
		elseif newType == "crystal" then
			newGoldType = 2160
		else
			return 0
		end	
		doPlayerRemoveItem(cid, goldType, count*difference)
		while gpstogive > 100 do
			gpstogive = gpstogive - 100
			doPlayerAddItem(cid, newGoldType, 100)
		end
		doPlayerAddItem(cid, newGoldType, gpstogive)
		return countToChange
	end
	return 0
end

function changeMoney(name, newMoney)
	local file = io.open("data/bank/"..name..".dat", "w")
	file:write("" .. newMoney .. "")
	file:close()
	return 1
end

function getAmountOfMoney(name)
	local file = io.open("data/bank/"..name..".dat", "r")
	if file == nil then 
		changeMoney(name, 0) 
		return 0
	end
	local x = file:read("*number")
	file:close()
	return x
end

function getPlayerMoney(cid)
	local crystalcoins = getPlayerItemCount(cid, 2160)*10000
	local platinumcoins = getPlayerItemCount(cid, 2152)*100
	local goldcoins = getPlayerItemCount(cid, 2148)
	return crystalcoins + platinumcoins + goldcoins
end

function depositAllMoney(name)
	local money = getPlayerMoney(getPlayerByName(name))
	local amountMoneyBank = getAmountOfMoney(name)
	changeMoney(name, amountMoneyBank + money)
	doPlayerRemoveMoney(getPlayerByName(name), money)
	return money
end

function getCountChangeGold(msg)
	-- made by Jiddo?
	local ret = 0
	local b, e = string.find(msg, "%d+")
	if b ~= nil and e ~= nil then
		ret = tonumber(string.sub(msg, b, e))
	end	
	return ret
end

function addMoney(cid, gpsrewarded)
	-- Made by Ispiro
	local crystalcoins = 0
	local platinumcoins = 0
	local goldcoins = 0	
	
	while gpsrewarded >= 10000 do 
		crystalcoins = crystalcoins + 1 
		gpsrewarded = gpsrewarded - 10000
	end
	while gpsrewarded >= 100 do
		platinumcoins = platinumcoins + 1 
		gpsrewarded = gpsrewarded - 100 
	end
	while gpsrewarded >= 1 do 
		goldcoins = goldcoins + 1
		gpsrewarded = gpsrewarded - 1 
	end
	doPlayerAddItemIspiro(cid, 2148, goldcoins)
	doPlayerAddItemIspiro(cid, 2152, platinumcoins)
	while(crystalcoins > 100) do
			doPlayerAddItemIspiro(cid, 2160, 100)
			crystalcoins = crystalcoins - 100
	end
	doPlayerAddItemIspiro(cid, 2160, crystalcoins)	
end

function doPlayerAddItemIspiro(cid, itemid, count)
	while(count > 100) do
		doPlayerAddItem(cid, itemid, 100)
		count = count - 100
	end
	doPlayerAddItem(cid, itemid, count)

end
 
Last edited:
If you can deposit and check your balance than withdraw should work perfectly fine since they all use the same function. Thanks for the comments, glad to see someone liked it.
 
Is there any max storage ?
topic : Thanks Inspiro, i guess many people will use it, much more than the count of posts.
 
No, there is no max storage. Data saved is in a txt file to ensure that it can be unlimited. Also, if you have a problem using the script, try making a folder called "bank" in the data folder and retry. If it still doesn't work, post your error. Credits for this part goes to Colex.
Btw, thanks for the encouraging post to make some more scripts :p
 
edited first post.
Code is now tested and sure to work.

Changes done:
• replaced getDistanceToCreature with getDistanceTo
• added the option to say no after you decide to deposit or withdraw.
• added doNpcSetCreatureFocus to onThink() so npc would face player
• fixed some bugs with banker options
• replaced amounToWithdraw and amountToDeposit with amountToHandle
• edited function changeGold a bit
• added function msgcontains for servers that dont have it

did some other changes in codes but i can't really remember them xD
anyways, credits to Sapphire for some of these changes done here
 
It works but could you try to add "transfer" that would be great
 
Back
Top