• 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 Super Simple "if and then" error will rep!

ziggy46802

Active Member
Joined
Aug 19, 2012
Messages
418
Reaction score
27
Okay, here is my script

bpquest2.lua
Code:
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) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) 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
 

 
 
if(msgcontains(msg, 'backpack')) and
	 (getPlayerStorageValue(cid, 1009) == 1) then
		setPlayerStorageValue(cid, 1009, 2)
		selfSay("Yes, I do sell backpacks, brown ones if thats {good} enough for you.", cid)
			else
				selfsay("What backpack are you talking about, you've lost your mind man.", cid)
			end
			end
				
if(msgcontains(msg, 'good')) then
	 if(getPlayerStorageValue(cid, 1009) > 1) then
		
		
		selfSay("Oh wow he did a terrible job on this piece of shit. Let me see if I can fix it up. Wait, all you have to do is this and that and this and this and bam! Here you go my friend, I hope you enjoy this piece of fuckin awesomeness.", cid)
		
		
		setPlayerStorageValue(cid, 1009, 3)
		setPlayerStorageValue(cid, 1011, 2)
		doPlayerAddItem(cid, 2365, 1)
		doPlayerRemoveItem(cid, 7342, 1)
		else
		selfSay("Good what? Lots of things are good, especially my shit I sell!", cid)
			end
			end
				
				
if(msgcontains(msg, 'more')) and
	 (getPlayerStorageValue(cid, 1011) == 2) then
		selfSay("What you want more? God damn man if you can't accept that gift you can't get nothin.", cid)
			end

				



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

-------------------------------------------------------------------------------

local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

shopModule:addBuyableItem({'brown backpack'}, 1988, 20, 1,'brown backpack')
shopModule:addBuyableItem({'brown bag'}, 1987, 5, 1,'brown bag')
shopModule:addBuyableItem({'fishing rod'}, 2580, 150, 1,'fishing rod')
shopModule:addBuyableItem({'machete'}, 2420, 35, 1,'machete')
shopModule:addBuyableItem({'pick'}, 2553, 50, 1,'pick')
shopModule:addBuyableItem({'rope'}, 2120, 50, 1,'rope')
shopModule:addBuyableItem({'scythe'}, 2550, 50, 1,'scythe')
shopModule:addBuyableItem({'shovel'}, 2554, 50, 1,'shovel')
shopModule:addBuyableItem({'worm'}, 3976, 1, 1,'worm')

npcHandler:addModule(FocusModule:new())

And I'm getting the error in my console this:

Lua Script Error: [Npc interface]
data/npc/scripts/bpquest2.lua

data/npc/scripts/lib/npc.lua:32: bag argument #1 to 'find' <string expected, got nil>
Warning: [NpcScript::NpcScript] Can not load script. data/npc/scripts/bpquest2.lua

npc.lua

I wrote "THIS IS LINE 32" where line 32 is so you can easily find it.
Code:
-- Include external classes.
dofile(getDataDir() .. 'npc/scripts/lib/npcsystem/npcsystem.lua')

-- Callback for isPremium(cid) so Jiddo's npcsystem works
isPlayerPremiumCallback = isPremium

-- get the distance to a creature
-- deprecated function
function getDistanceToCreature(id)
	debugPrint('getDistanceToCreature(): deprecated function. Use getDistanceTo()')
	return getDistanceTo(id)	
end

-- move to a creature
function moveToCreature(id)
	if(isCreature(id) == false) then
		debugPrint('moveToCreature(): creature not found.')
		return false
	end

	local pos = getCreaturePosition(id)
	selfMoveTo(pos.x, pos.y, pos.z)
	return true
end

function selfGotoIdle()
	debugPrint('selfGotoIdle(): deprecated function. Do not use it anymore!')
	return nil
end
 --LINE 31
 --THIS IS LINE 32 function msgcontains(message, keyword)
	local a, b = string.find(message, keyword)
	if a == nil or b == nil then
		return false
	end
	return true
end

function doCreatureSayWithDelay(cid,text,type,delay,e)
   if delay<=0 then
      doCreatureSay(cid,text,type)
   else
      local func=function(pars)
                    doCreatureSay(pars.cid,pars.text,pars.type)
                    pars.e.done=TRUE
                 end
      e.done=FALSE
      e.event=addEvent(func,delay,{cid=cid, text=text, type=type, e=e})
   end
end
 
--returns how many msgs he have said already
function cancelNPCTalk(events)
  local ret=1
  for aux=1,table.getn(events) do
     if events[aux].done==FALSE then
        stopEvent(events[aux].event)
     else
        ret=ret+1
     end
  end
  events=nil
  return(ret)
end
 
function doNPCTalkALot(msgs,interval)
  local e={}
  local ret={}
  if interval==nil then 
  interval=3000 --3 seconds is default time between messages
  end 
  for aux=1,table.getn(msgs) do
      e[aux]={}
      doCreatureSayWithDelay(getNpcCid(),msgs[aux],TALKTYPE_SAY,(aux-1)*interval,e[aux])
      table.insert(ret,e[aux])
  end
  return(ret)
end
 
try this:
LUA:
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) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) 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
 
if(msgcontains(msg, 'backpack')) and
	 if (getPlayerStorageValue(cid,1009) >= 1) then
		setPlayerStorageValue(cid, 1009, 1)
		selfSay("Yes, I do sell backpacks, brown ones if thats {good} enough for you.", cid)
			else
				selfsay("What backpack are you talking about, you've lost your mind man.", cid)
			end
			end
				
if(msgcontains(msg, 'good')) then

	 	if (getPlayerStorageValue(cid,1009) >= 1) then
      selfSay("Oh wow he did a terrible job on this piece of shit. Let me see if I can fix it up. Wait, all you have to do is this and that and this and this and bam! Here you go my friend, I hope you enjoy this piece of fuckin awesomeness.", cid)
      else
		setPlayerStorageValue(cid,1009,1)
		setPlayerStorageValue(cid, 1011, 2)
		doPlayerAddItem(cid, 2365, 1)
		doPlayerRemoveItem(cid, 7342, 1)
		else
		selfSay("Good what? Lots of things are good, especially my shit I sell!", cid)
			end
			end
				
				
if(msgcontains(msg, 'more')) and
	 if (getPlayerStorageValue(cid,1011) >= 2) then
		selfSay("What you want more? God damn man if you can't accept that gift you can't get nothin.", cid)
			end

				



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

-------------------------------------------------------------------------------

local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

shopModule:addBuyableItem({'brown backpack'}, 1988, 20, 1,'brown backpack')
shopModule:addBuyableItem({'brown bag'}, 1987, 5, 1,'brown bag')
shopModule:addBuyableItem({'fishing rod'}, 2580, 150, 1,'fishing rod')
shopModule:addBuyableItem({'machete'}, 2420, 35, 1,'machete')
shopModule:addBuyableItem({'pick'}, 2553, 50, 1,'pick')
shopModule:addBuyableItem({'rope'}, 2120, 50, 1,'rope')
shopModule:addBuyableItem({'scythe'}, 2550, 50, 1,'scythe')
shopModule:addBuyableItem({'shovel'}, 2554, 50, 1,'shovel')
shopModule:addBuyableItem({'worm'}, 3976, 1, 1,'worm')

npcHandler:addModule(FocusModule:new())
 
Sorry Cronox it wont let me rep you since Ive already repped you recently, it didn't work but it set me on the straight path and I did get it to work eventually so you did help me out, without your help I would have been going around in circles getting nowhere.

Thank you :w00t:

I just now got it to finally work after so many failed attempts, but here is the VERY basic script that he only responds to you the first time you do the quest for him, after that he just ignores you (because I could not get it so that he wouldn't say both things the first time or cheat and keep giving you the reward, etc., ran into many many problems but I figured it out in the end)

Here it is: an npc that takes an item from you and gives you another item for it

Code:
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) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) 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
 
if(msgcontains(msg, 'backpack')) and
	  (getPlayerStorageValue(cid,1009) == 2) then
		selfSay("Yes, I do sell backpacks, brown ones if thats {good} enough for you.", cid)
			elseif getPlayerStorageValue(cid, 1009) == 1 or getPlayerStorageValue(cid, 1009) == 3
				then selfSay("What backpack are you talking about, you've lost your mind man.", cid)
				elseif getPlayerStorageValue(cid, 1009) == 2 then
					setPlayerStorageValue(cid, 1009, 3)
			end
		
if(msgcontains(msg, 'good')) and
 
	 	 (getPlayerStorageValue(cid,1009) == 3) then
      selfSay("Oh wow he did a terrible job on this piece of shit. Let me see if I can fix it up. Wait, all you have to do is this and that and this and this and bam! Here you go my friend, I hope you enjoy this piece of fuckin awesomeness.", cid)
		setPlayerStorageValue(cid,1009,4)
		doPlayerAddItem(cid, 2365, 1)
		doPlayerRemoveItem(cid, 7342, 1)
			else
					end
 end
 
 
 
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
-------------------------------------------------------------------------------
 
local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)
 
shopModule:addBuyableItem({'brown backpack'}, 1988, 20, 1,'brown backpack')
shopModule:addBuyableItem({'brown bag'}, 1987, 5, 1,'brown bag')
shopModule:addBuyableItem({'fishing rod'}, 2580, 150, 1,'fishing rod')
shopModule:addBuyableItem({'machete'}, 2420, 35, 1,'machete')
shopModule:addBuyableItem({'pick'}, 2553, 50, 1,'pick')
shopModule:addBuyableItem({'rope'}, 2120, 50, 1,'rope')
shopModule:addBuyableItem({'scythe'}, 2550, 50, 1,'scythe')
shopModule:addBuyableItem({'shovel'}, 2554, 50, 1,'shovel')
shopModule:addBuyableItem({'worm'}, 3976, 1, 1,'worm')
 
npcHandler:addModule(FocusModule:new())
 
Last edited:
hmmm try this
LUA:
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) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) 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
 

 
 
if(msgcontains(msg, 'backpack')) and
	 (getPlayerStorageValue(cid, 1009) == 1) then
		setPlayerStorageValue(cid, 1009, 2)
		selfSay("Yes, I do sell backpacks, brown ones if thats {good} enough for you.", cid)
			else
				selfsay("What backpack are you talking about, you've lost your mind man.", cid)
			end
			end
				
if(msgcontains(msg, 'good')) then
	 (getPlayerStorageValue(cid,1009) > 1) then
		selfSay("Oh wow he did a terrible job on this piece of shit. Let me see if I can fix it up. Wait, all you have to do is this and that and this and this and bam! Here you go my friend, I hope you enjoy this piece of fuckin awesomeness.", cid)
        setPlayerStorageValue(cid, 1009, 3)
		setPlayerStorageValue(cid, 1011, 2)
		doPlayerAddItem(cid, 2365, 1)
		doPlayerRemoveItem(cid, 7342, 1)
		else
		selfSay("Good what? Lots of things are good, especially my shit I sell!", cid)
			end
			end
				
				
if(msgcontains(msg, 'more')) and
	 (getPlayerStorageValue(cid, 1011) == 2) then
		selfSay("What you want more? God damn man if you can't accept that gift you can't get nothin.", cid)
			end

				



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

-------------------------------------------------------------------------------

local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

shopModule:addBuyableItem({'brown backpack'}, 1988, 20, 1,'brown backpack')
shopModule:addBuyableItem({'brown bag'}, 1987, 5, 1,'brown bag')
shopModule:addBuyableItem({'fishing rod'}, 2580, 150, 1,'fishing rod')
shopModule:addBuyableItem({'machete'}, 2420, 35, 1,'machete')
shopModule:addBuyableItem({'pick'}, 2553, 50, 1,'pick')
shopModule:addBuyableItem({'rope'}, 2120, 50, 1,'rope')
shopModule:addBuyableItem({'scythe'}, 2550, 50, 1,'scythe')
shopModule:addBuyableItem({'shovel'}, 2554, 50, 1,'shovel')
shopModule:addBuyableItem({'worm'}, 3976, 1, 1,'worm')

npcHandler:addModule(FocusModule:new())
 
Back
Top