• 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.2] How to make npc that gives free gold

Tbol

Well-Known Member
Joined
Apr 7, 2019
Messages
526
Reaction score
54
I need a little bit more advanced version of it. I will try to explain
The max gold it can give is 3 gold. So situation if you have 1gold this npc would give only 2 gold now, if you have 2 gold it would give 1 gold now if you have 3 or 4 gold and more it wont give anything because you already have 3gold and it would allow to take it only once. Thats it
 
Solution
Sorry, completely forgot about this.
Here is an iteration of @Sasyia 's code

Note: I did not test the query at all, and I based the query of TFS 1.2 schema from the official repo

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 function creatureSayCallback(cid, type, msg)
    if not...
Lua:
local playerMoney = player:getMoney()
local storage = 10000
if msg == 'gold' and player:getStorageValue(storage) == -1 then
     if playerMoney < 3 then
           player:addMoney(3 - playerMoney)
           player:setStorageValue(storage, 1)
    end
end
 
Lua:
local playerMoney = player:getMoney()
local storage = 10000
if msg == 'gold' and player:getStorageValue(storage) == -1 then
     if playerMoney < 3 then
           player:addMoney(3 - playerMoney)
           player:setStorageValue(storage, 1)
    end
end
attempt to index global player (a nil value) this code part
local playerMoney = player:getMoney()
 
He probably did at bottom somewhere @Rombadr, try this:
Lua:
local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    if msgcontains(msg, "gold") or msgcontains(msg, "money") then
        local storage = Storage.Foo -- here path of your storage from storages.lua
        if player:getStorageValue(storage) < 0 then
            local playerMoney = player:getMoney()
            if playerMoney < 3 then
                player:addMoney(3 - playerMoney)
                player:setStorageValue(storage, 1)
                npcHandler:say("Here you go.", cid)
            end
        else
            npcHandler:say("You already took your money.", cid)
        end
    end
    return true
end
 
who cares about message count?
did you add it above the first line of code?
Ofc at the top
Post automatically merged:

He probably did at bottom somewhere @Rombadr, try this:
Lua:
local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    if msgcontains(msg, "gold") or msgcontains(msg, "money") then
        local storage = Storage.Foo -- here path of your storage from storages.lua
        if player:getStorageValue(storage) < 0 then
            local playerMoney = player:getMoney()
            if playerMoney < 3 then
                player:addMoney(3 - playerMoney)
                player:setStorageValue(storage, 1)
                npcHandler:say("Here you go.", cid)
            end
        else
            npcHandler:say("You already took your money.", cid)
        end
    end
    return true
end
Okay so nothing happens when you type gold
 
Last edited:
Please post full .lua file of this NPC
Please post full .lua file of this NPC
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 function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    if msgcontains(msg, "gold") or msgcontains(msg, "money") then
        local storage = Storage.Foo -- here path of your storage from storages.lua
        if player:getStorageValue(storage) < 0 then
            local playerMoney = player:getMoney()
            if playerMoney < 3 then
                player:addMoney(3 - playerMoney)
                player:setStorageValue(storage, 1)
                npcHandler:say("Here you go.", cid)
            end
        else
            npcHandler:say("You already took your money.", cid)
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
This is my code of NPC (tested and working):
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 function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local player = Player(cid)
    if msgcontains(msg, "gold") or msgcontains(msg, "money") then
        local storage = Storage.Foo -- here path of your storage from storages.lua
        if player:getStorageValue(storage) < 0 then
            local playerMoney = player:getMoney()
            if playerMoney < 3 then
                player:addMoney(3 - playerMoney)
                player:setStorageValue(storage, 1)
                npcHandler:say("Here you go.", cid)
            end
        else
            npcHandler:say("You already took your money.", cid)
        end
    end
    return true
end

npcHandler:addModule(FocusModule:new())
npcHandler:setMessage(MESSAGE_GREET, "Hello!")

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)
 
This is my code of NPC (tested and working):
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 function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local player = Player(cid)
    if msgcontains(msg, "gold") or msgcontains(msg, "money") then
        local storage = Storage.Foo -- here path of your storage from storages.lua
        if player:getStorageValue(storage) < 0 then
            local playerMoney = player:getMoney()
            if playerMoney < 3 then
                player:addMoney(3 - playerMoney)
                player:setStorageValue(storage, 1)
                npcHandler:say("Here you go.", cid)
            end
        else
            npcHandler:say("You already took your money.", cid)
        end
    end
    return true
end

npcHandler:addModule(FocusModule:new())
npcHandler:setMessage(MESSAGE_GREET, "Hello!")

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)
Okay works but how to make it that it would allow only one time per account too? so it wont be abusable and when you have this 3 gold it doesnt send any message that you have like 3 gold on you so it would be great to make those two adjustements too
 
No clue about making it one time per account, but about message, just add else with
npcHandler:say("message", cid) at 24 line
 
So you would have to add a column in your database (atleast so its clean) in the accounts table. Make it an int(1)

Then make a query to check if its 1 or 0 and make a query to set it to 1 if it is 0. If you need to know how let me know.
 
So you would have to add a column in your database (atleast so its clean) in the accounts table. Make it an int(1)

Then make a query to check if its 1 or 0 and make a query to set it to 1 if it is 0. If you need to know how let me know.
Hmm is there no other way without putting fingers on database?
 
Back
Top