• 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...
Hmm is there no other way without putting fingers on database?
Yes, querying the account, storages and player tables which imho is a better solution than changing the db, I’ll try and help you tomorrow since I’m on my phone right now
 
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.
Adding a column for one quest in the accounts table will not make the code clean. It will just bring an unwanted mess. It's better to write proper select for it.
 
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 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

                local stg_count_qry = db.storeQuery("SELECT COUNT(*) AS stg_count FROM accounts AS a JOIN players AS p ON p.account_id = a.id JOIN player_storage AS ps ON ps.player_id = p.id WHERE a.id = " .. player:getAccountId() .." AND ps.key = " .. storage .. " AND ps.value > 0")
                local stg_count = 1

                if stg_count_qry ~= false then
                    stg_count = result.getDataInt(stg_count_qry, "stg_count")
                    result.free(stg_count_qry)
                end

                if stg_count > 0 then
                    npcHandler:say("You already took your money with a different account, you cheeky bastard.", cid)
                else
                    player:addMoney(3 - playerMoney)
                    player:setStorageValue(storage, 1)
                    npcHandler:say("Here you go.", cid)
                end
            else
                npcHandler:say("You have more than enough!", 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)
 
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 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

                local stg_count_qry = db.storeQuery("SELECT COUNT(*) AS stg_count FROM accounts AS a JOIN players AS p ON p.account_id = a.id JOIN player_storage AS ps ON ps.player_id = p.id WHERE a.id = " .. player:getAccountId() .." AND ps.key = " .. storage .. " AND ps.value > 0")
                local stg_count = 1

                if stg_count_qry ~= false then
                    stg_count = result.getDataInt(stg_count_qry, "stg_count")
                    result.free(stg_count_qry)
                end

                if stg_count > 0 then
                    npcHandler:say("You already took your money with a different account, you cheeky bastard.", cid)
                else
                    player:addMoney(3 - playerMoney)
                    player:setStorageValue(storage, 1)
                    npcHandler:say("Here you go.", cid)
                end
            else
                npcHandler:say("You have more than enough!", 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)
Attempt to concatenate local 'storage' (a nil value) stack traceback:
[C]: in function '__concat`

in this line
Lua:
local stg_count_qry = db.storeQuery("SELECT COUNT(*) AS stg_count FROM accounts AS a JOIN players AS p ON p.account_id = a.id JOIN player_storage AS ps ON ps.player_id = p.id WHERE a.id = " .. player:getAccountId() .." AND ps.key = " .. storage .. " AND ps.value > 0")
 
Hmm is there no other way without putting fingers on database?

There is a way but it would be to store it in memory on the server. Which gets deleted when you turn the server off. That is why the database exists it holds anything that MUST be stored for longer than the servers life span. Hope that helps.
 
Attempt to concatenate local 'storage' (a nil value) stack traceback:
[C]: in function '__concat`

in this line
Lua:
local stg_count_qry = db.storeQuery("SELECT COUNT(*) AS stg_count FROM accounts AS a JOIN players AS p ON p.account_id = a.id JOIN player_storage AS ps ON ps.player_id = p.id WHERE a.id = " .. player:getAccountId() .." AND ps.key = " .. storage .. " AND ps.value > 0")
Have you changed this value?
Line #22
Lua:
local storage = Storage.Foo
 
Back
Top