https://otland.net/threads/how-to-tab-lua-scripts.203763/
Here's how to tab your current script properly. (or rather.. as well as possible.)
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, 'blood')) then
selfSay('Hello have any blood?', cid)
talkState[talkUser] = 1
elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
if (getPlayerStorageValue(cid,1237) > 0) then
else
if(doPlayerRemoveItem(cid, 21417, 1) == TRUE) then
setPlayerStorageValue(cid,1237,1)
Player:addExperience (cid, 10000000)
selfSay('Have you got any Blood Vial', cid)
else
selfSay(' Get Get Away Now! No Blood! No Help! ', cid)
end
-- never ends if statement
-- never ends if statement
return true
end
end -- bad end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
As we can see there are a few misplaced ends and else's, and a missing parameter if they are missing the item.
For what you want to do, you'd also want to remove the storage values, so the player can give the vial indefinitely.
Let's do both of those in sequence.
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, 'blood')) then
selfSay('Hello have any blood?', cid)
talkState[talkUser] = 1
elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
if (getPlayerStorageValue(cid,1237) > 0) then
else
if(doPlayerRemoveItem(cid, 21417, 1) == TRUE) then
setPlayerStorageValue(cid,1237,1)
Player:addExperience (cid, 10000000)
selfSay('Have you got any Blood Vial', cid)
else
selfSay(' Get Get Away Now! No Blood! No Help! ', cid)
end
end
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
This still doesn't make proper sense, but removing and changing some of the code we can fix that quickly enough.
Also the experience given is 10 million, not 10 thousand as you explained above. We will fix that oversight as well. (which is actually 10000/10 (1000 experience) per vial.)
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, 'blood')) then
selfSay('Hello have any blood?', cid)
talkState[talkUser] = 1
elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
if(doPlayerRemoveItem(cid, 21417, 1) == TRUE) then
Player:addExperience (cid, 1000)
selfSay('Thank you. I have taken 1 blood vial in return for 1000 experience. Come again!', cid)
else
selfSay(' Get Get Away Now! No Blood! No Help! ', cid)
end
talkState[talkUser] = 0 -- we need to remove the players talkstate after it's given
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Now we can give them the vial indefinitely.
But this can become tedious.. So we could change it to give 1 vial or all vials, yes?
Let's do that.
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, 'blood')) then
selfSay('Hello have any blood?', cid)
talkState[talkUser] = 1
elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
if getPlayerItemCount(cid, 21417) >= 1 then
selfSay('Good mortal. Now are you going to give me {1 vial} or {all vials}?', cid)
talkState[talkUser] = 2
else
selfSay('Get Get Away Now! No Blood! No Help!', cid)
talkState[talkUser] = 0
end
elseif(msgcontains(msg, '1') and talkState[talkUser] == 2) then
if(doPlayerRemoveItem(cid, 21417, 1) == TRUE) then
Player:addExperience (cid, 1000)
selfSay('Thank you. I have taken 1 blood vial in return for 1000 experience. Come again!', cid)
else
selfSay('Get Get Away Now! No Blood! No Help!', cid)
end
talkState[talkUser] = 0
elseif(msgcontains(msg, 'all') and talkState[talkUser] == 2) then
if getPlayerItemCount(cid, 21417) >= 1 then
amount = getPlayerItemCount(cid, 21417)
Player:addExperience (cid, (1000 * amount))
doPlayerRemoveItem(cid, 21417, amount)
selfSay('Thank you. I have taken all your blood vials in return for experience. Come again!', cid)
else
selfSay('Get Get Away Now! No Blood! No Help!', cid)
end
talkState[talkUser] = 0
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
As you can see it's all relative to what you wish to do.
Now of course you could always add more, but I think this should be enough.
Note: none of this has been tested.
Also note: you are using 1.x, however I assume your compat file is responsible for allowing you to use 0.3.x scripting.
You'll want to learn how to use 1.x scripting style in the future to avoid possible confusion.