Part 3 - Using Storage
Note: There are different functions for this, but I will use getPlayerStorageValue and setPlayerStorageValue here because it works in both Crying Damson (Because of the 100-compat.lua) and Mystic Spirit.
If you want to make an NPC that gives a mission, you should always use storage, because the NPC has to know if a player already accepted the mission or completed it.
Storage gets saved in the database, so this stays forever (unless you change this with a script or in the database).
To set storage.
Code:
setPlayerStorageValue(cid, 5000, 1)
To check the value of storage.
Code:
if getPlayerStorageValue(cid, 5000) == 1 then
The 5000 here is the storage, the 1 here is the value of the storage (both the storage number and value are as example, use a storage number that is not used in an other script).
When you set storage, you only have to change the value.
Also something good to use.
This means if you use the word storage, it means 5000, so if you want to change the 5000 later and you use it alot in your script, you only have to change it 1x.
When using storage, after every msgcontains line you have to check for storagevalue, because it should respond different after accepting or completing the mission.
First time people go the the NPC, they don't have the storage yet, storagevalue -1 means the player doesn't have the storage so you can start with that.
Code:
if getPlayerStorageValue(cid, storage) == -1 then
selfSay('My daughter lost her doll, I saw a monster taking it that went in this cave, but I\'m to affraid to go there, can you get it for me?.', cid)
end
Then you set the storagevalue to 1 after the player accepted te mission.
Code:
setPlayerStorageValue(cid, storage, 1)
Now the player won't get the 'My daughter lost her doll..' message when saying mission again, because the storagevalue is then not -1 anymore.
Code:
local storage = 5000
if msgcontains(msg, "mission") then
if getPlayerStorageValue(cid, storage) == -1 then
selfSay("My daughter lost her doll, I saw a monster taking it that went in this cave, but I'm to affraid to go there, can you get it for me?", cid)
talkState[talkUser] = 1
end
elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
if getPlayerStorageValue(cid, storage) == -1 then
selfSay("Thanks alot, come back when you have it.", cid)
setPlayerStorageValue(cid, storage, 1)
end
talkState[talkUser] = 0
end
The storagevalue after accepting the mission was set to 1, so now you add a response if the storagevalue is 1 for when the player comes back.
When the players says yes when the storagevalue is 1, you add what the NPC should do, in this case removing the item (doll) and add rewards.
Then you can set the storage value to 2, so when the player says mission again, it won't respond on storagevalue 1 anymore.
Code:
local storage = 5000
if msgcontains(msg, "mission") then
if getPlayerStorageValue(cid, storage) == -1 then -- When the player comes to the NPC for the first time or didn't accepted the mission yet.
selfSay("My daughter lost her doll, I saw a monster taking it that went in this cave, but I'm to affraid to go there, can you get it for me?", cid)
talkState[talkUser] = 1
elseif getPlayerStorageValue(cid, storage) == 1 then -- When the player comes back after accepting the mission.
selfSay("Did you find the doll?", cid)
talkState[talkUser] = 1
else
selfSay("Thanks again for finding the doll.", cid) -- The NPC will say this when the storage is not -1 or 1, so this means the player completed the mission.
end
elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
if getPlayerStorageValue(cid, storage) == -1 then
selfSay("Thanks alot, come back when you have it.", cid)
setPlayerStorageValue(cid, storage, 1)
else -- You can use else here because it's only possible to say yes here with storagevalue -1 and 1, so if it's not -1, it has to be 1.
if doPlayerRemoveItem(cid, 2110, 1) then -- You can use this to remove an item, by using if it will also look if it's possible, so it's not true if the player doesn't have the item.
selfSay("That's great, my daughter will be really happy, thanks.", cid)
doPlayerAddItem(cid, 2160, 3) -- You can add for example items as reward.
doPlayerAddExp(cid, 5000) -- or experience
setPlayerStorageValue(cid, 3843, 1) -- or storage, for example to open a quest door (I will not add this to the in total part, since most of the time you don't need it).
setPlayerStorageValue(cid, storage, 2) -- Set storage to value 2, so when the value is not -1 or 1, it will say 'Thanks again for finding the doll.'.
else -- if removing the item wasn't possible (so this means the player doesn't have it), it will do the part after else.
selfSay("You don't have it.", cid)
end
end
talkState[talkUser] = 0
end
In total
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
local storage = 5000
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, "mission") then
if getPlayerStorageValue(cid, storage) == -1 then
selfSay("My daughter lost her doll, I saw a monster taking it that went in this cave, but I\'m to affraid to go there, can you get it for me?", cid)
talkState[talkUser] = 1
elseif getPlayerStorageValue(cid, storage) == 1 then
selfSay("Did you find the doll?", cid)
talkState[talkUser] = 1
else
selfSay("Thanks again for finding the doll.", cid)
end
elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
if getPlayerStorageValue(cid, storage) == -1 then
selfSay("Thanks alot, come back when you have it.", cid)
setPlayerStorageValue(cid, storage, 1)
else
if(doPlayerRemoveItem(cid, 2110, 1)) then
selfSay("That's great, my daughter will be really happy, thanks.", cid)
doPlayerAddItem(cid, 2160, 3)
doPlayerAddExp(cid, 5000)
setPlayerStorageValue(cid, storage, 2)
else
selfSay("You don't have it.", cid)
end
end
talkState[talkUser] = 0
elseif msgcontains(msg, "no") and talkState[talkUser] == 1 then
selfSay("Ok then.", cid)
talkState[talkUser] = 0
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
You can add local storage = 5000 above function creatureSayCallback(cid, type, msg), because it doesn't have any of the functions parameters (cid, type, msg), so it doesn't have to be in the function.
Ingame example