• 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 [TFS 1.3] storage on npc

Landera

Veteran OT User
Joined
Nov 24, 2011
Messages
905
Solutions
1
Reaction score
318
Good evening,

This evening i was struggling with my NPC file and after I succesfully added the items and 3 items to choose from I've hit a solid wall.
I cant figure out to put a storage on it, so the player can only do this once and can only pick the one weapon he choose from.

WARNING I'm not a great scripter so the code can look messy

Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local Topic = {}

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
    elseif msgcontains(msg, 'job') or msgcontains(msg, 'mission') then
        npcHandler:say('I keep an eye on the armory, and {weaponry} so people won\'t just grab what they want.', cid)
        Topic[cid] = nil
    elseif msgcontains(msg, 'weaponry') or msgcontains(msg, 'weapon') then
        npcHandler:say('I could gift you an {sword}, {axe} or a {club} weapon if you gather some {items} for me so I can see ur worthy enough.', cid)
        Topic[cid] = nil  
    elseif msgcontains(msg, 'items') or msgcontains(msg, 'item') then
        npcHandler:say('I need the following items five cyclops toes, five wolf paws and a battle shield, you can only pick one reward.', cid)
        Topic[cid] = nil              
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
        elseif msgcontains(msg, 'sword') then
        if getPlayerItemCount(cid,10574) >= 5 and getPlayerItemCount(cid,5897) >= 5 and getPlayerItemCount(cid,2513) >= 1 then
        if doPlayerRemoveItem(cid,10574,5) and doPlayerRemoveItem(cid,5897,5) and doPlayerRemoveItem(cid,2513,1) then
            npcHandler:say('Oh, that\'s amazing! Please take this as reward!', cid)
            doPlayerAddItem(cid, 8602, 1)
            setPlayerStorageValue(cid, 8602, 1)          
            end
        else
            npcHandler:say('You don\'t have all the required items.', cid)
        end
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
        elseif msgcontains(msg, 'axe') then
        if getPlayerItemCount(cid,10574) >= 5 and getPlayerItemCount(cid,5897) >= 5 and getPlayerItemCount(cid,2513) >= 1 then
        if doPlayerRemoveItem(cid,10574,5) and doPlayerRemoveItem(cid,5897,5) and doPlayerRemoveItem(cid,2513,1) then
            npcHandler:say('Oh, that\'s amazing! Please take this as reward!', cid)
            doPlayerAddItem(cid, 8601, 1)
            setPlayerStorageValue(cid, 8602, 1)
            end
        else
            npcHandler:say('You don\'t have all the required items.', cid)
        end
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
        elseif msgcontains(msg, 'club') then
        if getPlayerItemCount(cid,10574) >= 5 and getPlayerItemCount(cid,5897) >= 5 and getPlayerItemCount(cid,2513) >= 1 then
        if doPlayerRemoveItem(cid,10574,5) and doPlayerRemoveItem(cid,5897,5) and doPlayerRemoveItem(cid,2513,1) then
            npcHandler:say('Oh, that\'s amazing! Please take this as reward!', cid)
            doPlayerAddItem(cid, 2439, 1)
            setPlayerStorageValue(cid, 8602, 1)          
            end
        else
            npcHandler:say('You don\'t have all the required items.', cid)
        end
        Topic[cid] = nil
    end
    return true
end


npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
B
You just need to simply check the storage value using getPlayerStorageValue.

So it would look like this:
Lua:
elseif msgcontains(msg, 'sword') then
    if getPlayerStorageValue(cid, 8602) == -1 then -- check the value is -1 (not set)
        if getPlayerItemCount(cid,10574) >= 5 and getPlayerItemCount(cid,5897) >= 5 and getPlayerItemCount(cid,2513) >= 1 then
            if doPlayerRemoveItem(cid,10574,5) and doPlayerRemoveItem(cid,5897,5) and doPlayerRemoveItem(cid,2513,1) then
                npcHandler:say('Oh, that\'s amazing! Please take this as reward!', cid)
                doPlayerAddItem(cid, 8602, 1)
                setPlayerStorageValue(cid, 8602, 1) -- we set the value to 1 here       
            end
        else...
You just need to simply check the storage value using getPlayerStorageValue.

So it would look like this:
Lua:
elseif msgcontains(msg, 'sword') then
    if getPlayerStorageValue(cid, 8602) == -1 then -- check the value is -1 (not set)
        if getPlayerItemCount(cid,10574) >= 5 and getPlayerItemCount(cid,5897) >= 5 and getPlayerItemCount(cid,2513) >= 1 then
            if doPlayerRemoveItem(cid,10574,5) and doPlayerRemoveItem(cid,5897,5) and doPlayerRemoveItem(cid,2513,1) then
                npcHandler:say('Oh, that\'s amazing! Please take this as reward!', cid)
                doPlayerAddItem(cid, 8602, 1)
                setPlayerStorageValue(cid, 8602, 1) -- we set the value to 1 here       
            end
        else
            npcHandler:say('You don\'t have all the required items.', cid)
        end
    else
        npcHandler:say('You have already taken your reward.', cid) -- message if item is already taken
    end

You will have to do this for all 3 items
 
Solution
You just need to simply check the storage value using getPlayerStorageValue.

So it would look like this:
Lua:
elseif msgcontains(msg, 'sword') then
    if getPlayerStorageValue(cid, 8602) == -1 then -- check the value is -1 (not set)
        if getPlayerItemCount(cid,10574) >= 5 and getPlayerItemCount(cid,5897) >= 5 and getPlayerItemCount(cid,2513) >= 1 then
            if doPlayerRemoveItem(cid,10574,5) and doPlayerRemoveItem(cid,5897,5) and doPlayerRemoveItem(cid,2513,1) then
                npcHandler:say('Oh, that\'s amazing! Please take this as reward!', cid)
                doPlayerAddItem(cid, 8602, 1)
                setPlayerStorageValue(cid, 8602, 1) -- we set the value to 1 here      
            end
        else
            npcHandler:say('You don\'t have all the required items.', cid)
        end
    else
        npcHandler:say('You have already taken your reward.', cid) -- message if item is already taken
    end

You will have to do this for all 3 items

Thanks my man, works like a charm. Seems like I was kinda close.
 
Thanks my man, works like a charm. Seems like I was kinda close.
Here is a slightly different approach to coding, to make it easier to read, and avoid nested if-else statements.

The goal is to make each part of the code a single block with a single purpose.
If the statement is true, then stop the code.
If every statement is false, you'll get to the end result.

It's slightly backwards when you're thinking about it, but it makes your code easier to read and troubleshoot in the long run.

Second script just has greentext to explain the reasoning behind the layout
Lua:
elseif msgcontains(msg, 'sword') then
    if getPlayerStorageValue(cid, 8602) ~= -1 then
        npcHandler:say("You have already taken your reward.", cid)
        return true
    end
    if getPlayerItemCount(cid, 10574) < 5 or getPlayerItemCount(cid, 5897) < 5 or getPlayerItemCount(cid, 2513) < 1 then
        npcHandler:say("You don't have all the required items.", cid)
        return true
    end 
    doPlayerRemoveItem(cid, 10574, 5)
    doPlayerRemoveItem(cid, 5897, 5)
    doPlayerRemoveItem(cid, 2513, 1)
    doPlayerAddItem(cid, 8602, 1)
    setPlayerStorageValue(cid, 8602, 1)
    npcHandler:say("Oh, that's amazing! Please take this as reward!", cid)
    return true
Lua:
elseif msgcontains(msg, 'sword') then

    -- here we check if the storage value has been changed previously.
    -- We know that the default value of a storage is -1
    -- so if check that the value is NOT equal to -1, that means this 'quest' has been completed..
    -- and we can end the script here, after telling the player some information
    if getPlayerStorageValue(cid, 8602) ~= -1 then
        npcHandler:say("You have already taken your reward.", cid)
        return true
    end
 
    -- so same logic as above
    -- if the player has LESS then the required amount of any item we want to remove from them..
    -- then we send them to the error message of not having enough items, then end the script
    if getPlayerItemCount(cid, 10574) < 5 or getPlayerItemCount(cid, 5897) < 5 or getPlayerItemCount(cid, 2513) < 1 then
        npcHandler:say("You don't have all the required items.", cid)
        return true
    end
 
    -- since we have already checked that the player has the required item previously, we can directly remove them
    doPlayerRemoveItem(cid, 10574, 5)
    doPlayerRemoveItem(cid, 5897, 5)
    doPlayerRemoveItem(cid, 2513, 1)
 
    -- after removing the items, we give them their reward, and update their storage value
    doPlayerAddItem(cid, 8602, 1)
    setPlayerStorageValue(cid, 8602, 1)
 
    -- and let the player know something has happened
    npcHandler:say("Oh, that's amazing! Please take this as reward!", cid)
    return true
 
Back
Top