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

NPC script request

azzkaban

Monster Maker
Joined
Feb 23, 2010
Messages
1,101
Reaction score
195
Location
Iquique Chile
Hello, I need NPC for buy cap.

Example:

first cap
local config = {

amount = 100, -- Cap to add.
price = 10000 -- Price of it in gp.
storage = 100001 -- storage for first cap.

}

second cap
local config = {

amount = 200, -- Cap to add.
price = 20000 -- Price of it in gp.
storage = 100002 -- storage for first cap.

}

text to no more cap to sell: "sorry, you dont buy more cap".

Thank you in advanced and rep+
 
I have not tested this... so please..
Test it and post errors in here and I'll try to fix it.

I assume you're using TFS, I never works with it.. so I'm not fully sure about scripts to it, but I'll get it to work in one way or another =)

Cap Seller

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

function creatureSayCallback(cid, type, msg) msg = string.lower(msg)		
    if(npcHandler.focus ~= cid) then
        return false
    end
config = {
	cap_to_add = 100, --How much cap should he get?
		price = 10000, --How much should he pay, in Gps?
			storage = 100001, --The storage that saves/check how many times he bought cap
				turns = 2, --how many times can he buy?
					extra_cap_per_turn = 0, --How much "bonus" cap will he get per turn?
						extra_price_per_turn = 0 --How much more will it cost, everytime he uses it?
}

if msgcontains(msg, 'cap') then
	if getPlayerStorageValue(cid, config.storage) < config.turns then
times_bought = getPlayerStorageValue(cid, config.storage)
new_price = -(extra_price_per_turn) --Don't touch this!
new_cap = -(extra_price_per_turn) --Don't touch this!
		while(times_bought < config.turns) do --Calculate the price
		new_price = (new_price+config.price+config.extra_price_per_turn)
		end
		while(times_bought < config.turns) do --Calculate the cap
		new_cap = (new_cap+config.cap_to_add+config.extra_cap_per_turn)
		end
		selfSay("For ".. new_price .." gold I will add ".. new_cap .." capacity, would you like that?", cid)
		talk_state[cid] = 101		
	else
	selfSay("Sorry, you cannot buy more cap!", cid)
	talk_state[cid] = 0
	
	end
	
elseif msgcontains(msg, 'yes') and talk_state[cid] = 101 then
	if getPlayerMoney(cid) >= new_price then
		doPlayerRemoveMoney(cid, new_price)
		doPlayerSetMaxCapacity(cid, getPlayerMaxCap(cid)+new_cap)
	selfSay("Pleasant doing business with you!", cid)
	talk_state[cid] = 0	
	else
	selfSay("Sorry, you don't have enough money!", cid)
	talk_state[cid] = 0		
	end
end

end
npcHandler:addModule(FocusModule:new())

Don't forget the rep++ if you found me helpful :)
 
Last edited:
I have not tested this... so please..
Test it and post errors in here and I'll try to fix it.

I assume you're using TFS, I never works with it.. so I'm not fully sure about scripts to it, but I'll get it to work in one way or another =)

Cap Seller

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

function creatureSayCallback(cid, type, msg) msg = string.lower(msg)		
    if(npcHandler.focus ~= cid) then
        return false
    end
config = {
	cap_to_add = 100, --How much cap should he get?
		price = 10000, --How much should he pay, in Gps?
			storage = 100001, --The storage that saves/check how many times he bought cap
				turns = 2, --how many times can he buy?
					extra_cap_per_turn = 0, --How much "bonus" cap will he get per turn?
						extra_price_per_turn = 0 --How much more will it cost, everytime he uses it?
}

if msgcontains(msg, 'cap') then
	if getPlayerStorageValue(cid, config.storage) < config.turns then
times_bought = getPlayerStorageValue(cid, config.storage)
new_price = -(extra_price_per_turn) --Don't touch this!
new_cap = -(extra_price_per_turn) --Don't touch this!
		while(times_bought < config.turns) do --Calculate the price
		new_price = (new_price+config.price+config.extra_price_per_turn)
		end
		while(times_bought < config.turns) do --Calculate the cap
		new_cap = (new_cap+config.cap_to_add+config.extra_cap_per_turn)
		end
		selfSay("For ".. new_price .." gold I will add ".. new_cap .." capacity, would you like that?", cid)
		talk_state[cid] = 101		
	else
	selfSay("Sorry, you cannot buy more cap!", cid)
	talk_state[cid] = 0
	
	end
	
elseif msgcontains(msg, 'yes') and talk_state[cid] = 101 then
	if getPlayerMoney(cid) >= new_price then
		doPlayerRemoveMoney(cid, new_price)
		doPlayerSetMaxCapacity(cid, getPlayerMaxCap(cid)+new_cap)
	selfSay("Pleasant doing business with you!", cid)
	talk_state[cid] = 0	
	else
	selfSay("Sorry, you don't have enough money!", cid)
	talk_state[cid] = 0		
	end
end

end
npcHandler:addModule(FocusModule:new())

Don't forget the rep++ if you found me helpful :)


Thank you, but

[Error - LuaInterface::loadFile] data/npc/scripts/Cap Seller.lua:42: 'then' expected near '='
[6/7/2012 11:57:7] [Warning - NpcEvents::NpcEvents] Cannot load script: data/npc/scripts/Cap Seller.lua
[6/7/2012 11:57:7] data/npc/scripts/Cap Seller.lua:42: 'then' expected near '='
 
Thank you, but

[Error - LuaInterface::loadFile] data/npc/scripts/Cap Seller.lua:42: 'then' expected near '='
[6/7/2012 11:57:7] [Warning - NpcEvents::NpcEvents] Cannot load script: data/npc/scripts/Cap Seller.lua
[6/7/2012 11:57:7] data/npc/scripts/Cap Seller.lua:42: 'then' expected near '='

Sorry slow reply, was out last night, here's the fix:

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
 
function creatureSayCallback(cid, type, msg) msg = string.lower(msg)		
    if(npcHandler.focus ~= cid) then
        return false
    end
config = {
	cap_to_add = 100, --How much cap should he get?
		price = 10000, --How much should he pay, in Gps?
			storage = 100001, --The storage that saves/check how many times he bought cap
				turns = 2, --how many times can he buy?
					extra_cap_per_turn = 0, --How much "bonus" cap will he get per turn?
						extra_price_per_turn = 0 --How much more will it cost, everytime he uses it?
}
 
if msgcontains(msg, 'cap') then
	if getPlayerStorageValue(cid, config.storage) < config.turns then
times_bought = getPlayerStorageValue(cid, config.storage)
new_price = -(extra_price_per_turn) --Don't touch this!
new_cap = -(extra_price_per_turn) --Don't touch this!
		while(times_bought < config.turns) do --Calculate the price
		new_price = (new_price+config.price+config.extra_price_per_turn)
		end
		while(times_bought < config.turns) do --Calculate the cap
		new_cap = (new_cap+config.cap_to_add+config.extra_cap_per_turn)
		end
		selfSay("For ".. new_price .." gold I will add ".. new_cap .." capacity, would you like that?", cid)
		talk_state[cid] = 101		
	else
	selfSay("Sorry, you cannot buy more cap!", cid)
	talk_state[cid] = 0
 
	end
 
elseif msgcontains(msg, 'yes') and talk_state[cid] == 101 then
	if getPlayerMoney(cid) >= new_price then
		doPlayerRemoveMoney(cid, new_price)
		doPlayerSetMaxCapacity(cid, getPlayerMaxCap(cid)+new_cap)
	selfSay("Pleasant doing business with you!", cid)
	talk_state[cid] = 0	
	else
	selfSay("Sorry, you don't have enough money!", cid)
	talk_state[cid] = 0		
	end
end
 
end
npcHandler:addModule(FocusModule:new())
 
Sorry slow reply, was out last night, here's the fix:

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
 
function creatureSayCallback(cid, type, msg) msg = string.lower(msg)		
    if(npcHandler.focus ~= cid) then
        return false
    end
config = {
	cap_to_add = 100, --How much cap should he get?
		price = 10000, --How much should he pay, in Gps?
			storage = 100001, --The storage that saves/check how many times he bought cap
				turns = 2, --how many times can he buy?
					extra_cap_per_turn = 0, --How much "bonus" cap will he get per turn?
						extra_price_per_turn = 0 --How much more will it cost, everytime he uses it?
}
 
if msgcontains(msg, 'cap') then
	if getPlayerStorageValue(cid, config.storage) < config.turns then
times_bought = getPlayerStorageValue(cid, config.storage)
new_price = -(extra_price_per_turn) --Don't touch this!
new_cap = -(extra_price_per_turn) --Don't touch this!
		while(times_bought < config.turns) do --Calculate the price
		new_price = (new_price+config.price+config.extra_price_per_turn)
		end
		while(times_bought < config.turns) do --Calculate the cap
		new_cap = (new_cap+config.cap_to_add+config.extra_cap_per_turn)
		end
		selfSay("For ".. new_price .." gold I will add ".. new_cap .." capacity, would you like that?", cid)
		talk_state[cid] = 101		
	else
	selfSay("Sorry, you cannot buy more cap!", cid)
	talk_state[cid] = 0
 
	end
 
elseif msgcontains(msg, 'yes') and talk_state[cid] == 101 then
	if getPlayerMoney(cid) >= new_price then
		doPlayerRemoveMoney(cid, new_price)
		doPlayerSetMaxCapacity(cid, getPlayerMaxCap(cid)+new_cap)
	selfSay("Pleasant doing business with you!", cid)
	talk_state[cid] = 0	
	else
	selfSay("Sorry, you don't have enough money!", cid)
	talk_state[cid] = 0		
	end
end
 
end
npcHandler:addModule(FocusModule:new())


Thank you! But:
11:06 Cap Seller: Hello Azzkara. do you need more cap? say cap.
11:06 Azzkara [16]: cap

---- and no sell, maybe I need xml file?
 
are you sure the correct NPC are linked to this lua script?

XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Cap Seller" script="Cap Seller.lua" walkinterval="600">
	<health now="100" max="100"/>
	<look type="131" head="58" body="43" legs="38" feet="76" addons="0"/>
	<parameters>
		<parameter key="message_greet" value="Hello |PLAYERNAME|. do you need more cap? say {cap}."/>
	</parameters>
</npc>
 
XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Cap Seller" script="Cap Seller.lua" walkinterval="600">
	<health now="100" max="100"/>
	<look type="131" head="58" body="43" legs="38" feet="76" addons="0"/>
	<parameters>
		<parameter key="message_greet" value="Hello |PLAYERNAME|. do you need more cap? say {cap}."/>
	</parameters>
</npc>

got teamviewer?
 
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 t = {}

local c = {
	amount = 100, -- Cap to add.
	price = 10000, -- Price of it in gp.
	storage = 100001,
	amount2 = 200, -- Cap to add.
	price2 =  20000,
}
function creatureSayCallback(cid, type, msg)
local str = math.max(0, getCreatureStorage(cid, c.storage))
	if not npcHandler:isFocused(cid) then
		return false
	elseif msgcontains(msg, 'cap') then
		selfSay("For ".. (str == 0 and c.price or c.price2) .." gold I will add ".. (str == 0 and c.amount or c.amount2) .." capacity, would you like that?", cid)
		t[cid] = 1
	elseif t[cid] == 1 and msgcontains(msg, 'yes') then
		if str ~= 2 then
			if getPlayerMoney(cid) >= (str == 0 and c.amount or c.amount2) then
				doPlayerSave(cid)
				local q = db.getResult("SELECT `cap` FROM `players` WHERE `id` = ".. getPlayerGUID(cid) .." LIMIT 1;"):getDataInt("cap")
				doPlayerRemoveMoney(cid, (str == 0 and c.amount or c.amount2))
				doPlayerSetMaxCapacity(cid, q+(str == 0 and c.amount or c.amount2))
				doCreatureSetStorage(cid, c.storage, math.max(0, getCreatureStorage(cid, c.storage)) + 1)
				selfSay("Pleasant doing business with you!", cid)
				t[cid] = nil
			else
				selfSay("Sorry, you don't have enough money!", cid)
				t[cid] = nil
			end
		else
			selfSay('Sorry, you can\'t buy more cap', cid)
		end
	end
	return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
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 t = {}

local c = {
	amount = 100, -- Cap to add.
	price = 10000, -- Price of it in gp.
	storage = 100001,
	amount2 = 200, -- Cap to add.
	price2 =  20000,
}
function creatureSayCallback(cid, type, msg)
local str = math.max(0, getCreatureStorage(cid, c.storage))
	if not npcHandler:isFocused(cid) then
		return false
	elseif msgcontains(msg, 'cap') then
		selfSay("For ".. (str == 0 and c.price or c.price2) .." gold I will add ".. (str == 0 and c.amount or c.amount2) .." capacity, would you like that?", cid)
		t[cid] = 1
	elseif t[cid] == 1 and msgcontains(msg, 'yes') then
		if str ~= 2 then
			if getPlayerMoney(cid) >= (str == 0 and c.amount or c.amount2) then
				doPlayerSave(cid)
				local q = db.getResult("SELECT `cap` FROM `players` WHERE `id` = ".. getPlayerGUID(cid) .." LIMIT 1;"):getDataInt("cap")
				doPlayerRemoveMoney(cid, (str == 0 and c.amount or c.amount2))
				doPlayerSetMaxCapacity(cid, q+(str == 0 and c.amount or c.amount2))
				doCreatureSetStorage(cid, c.storage, math.max(0, getCreatureStorage(cid, c.storage)) + 1)
				selfSay("Pleasant doing business with you!", cid)
				t[cid] = nil
			else
				selfSay("Sorry, you don't have enough money!", cid)
				t[cid] = nil
			end
		else
			selfSay('Sorry, you can\'t buy more cap', cid)
		end
	end
	return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())


npc dont says nothing... maybe I need XML npc?
 
XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Cap" nameDescription="cap" script="cap.lua" walkinterval="2000" floorchange="0" skull="green">
	<health now="100" max="100"/>
	<look type="130" head="39" body="122" legs="125" feet="57" addons="0"/>
	<parameters>
		<parameter key="message_greet" value="Hello |PLAYERNAME|. I sell cap."/>
	</parameters>
</npc>

now goto /npc/scripts/cap.lua

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 t = {}

local c = {
	amount = 100, -- Cap to add.
	price = 10000, -- Price of it in gp.
	storage = 100001,
	amount2 = 200, -- Cap to add.
	price2 =  20000,
}
function creatureSayCallback(cid, type, msg)
local str = math.max(0, getCreatureStorage(cid, c.storage))
	if not npcHandler:isFocused(cid) then
		return false
	elseif msgcontains(msg, 'cap') then
		selfSay("For ".. (str == 0 and c.price or c.price2) .." gold I will add ".. (str == 0 and c.amount or c.amount2) .." capacity, would you like that?", cid)
		t[cid] = 1
	elseif t[cid] == 1 and msgcontains(msg, 'yes') then
		if str ~= 2 then
			if getPlayerMoney(cid) >= (str == 0 and c.amount or c.amount2) then
				doPlayerSave(cid)
				local q = db.getResult("SELECT `cap` FROM `players` WHERE `id` = ".. getPlayerGUID(cid) .." LIMIT 1;"):getDataInt("cap")
				doPlayerRemoveMoney(cid, (str == 0 and c.amount or c.amount2))
				doPlayerSetMaxCapacity(cid, q+(str == 0 and c.amount or c.amount2))
				doCreatureSetStorage(cid, c.storage, math.max(0, getCreatureStorage(cid, c.storage)) + 1)
				selfSay("Pleasant doing business with you!", cid)
				t[cid] = nil
			else
				selfSay("Sorry, you don't have enough money!", cid)
				t[cid] = nil
			end
		else
			selfSay('Sorry, you can\'t buy more cap', cid)
		end
	end
	return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
[14/8/2012 9:49:35] [Error - NpcScript Interface]
[14/8/2012 9:49:35] data/npc/scripts/cap.lua:onCreatureSay
[14/8/2012 9:49:35] Description:
[14/8/2012 9:49:35] data/npc/scripts/cap.lua:21: bad argument #3 to 'max' (number expected, got nil)
[14/8/2012 9:49:35] stack traceback:
[14/8/2012 9:49:35] [C]: in function 'max'
[14/8/2012 9:49:35] data/npc/scripts/cap.lua:21: in function 'callback'
[14/8/2012 9:49:35] data/npc/lib/npcsystem/npchandler.lua:430: in function 'onCreatureSay'
[14/8/2012 9:49:35] data/npc/scripts/cap.lua:7: in function <data/npc/scripts/cap.lua:7>

Near! :D
 
Back
Top