• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Npc trade gold for premium points

Tbol

Well-Known Member
Joined
Apr 7, 2019
Messages
625
Reaction score
71
Tfs 1.x
Need npc that trades 10gold to 1000 premium points so if you have like 40gold you would get 4000 premium points and etc. And level requirement min 150lvl
 
Solution
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local amount = {}

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

npcHandler:addModule(FocusModule:new())

local function greetCallback(cid)
    amount[cid] = nil
    return true
end

local function isValidPointsAmount(points)
    if isNumber(points) == TRUE and points > 0 and points < 999999999...
Add this item, edit it to your requirements and make the npc sell this item.
 
Add this item, edit it to your requirements and make the npc sell this item.
But thats the hardest part :D to check how much he have x gold and send it x amount point i know how to check if he have 10gold and give 1k point if he does but then how to do calculations if he have like 60 gold or 500gold it should give 6k points or 50k points you know like automatic calculation. You know what i mean
 
But thats the hardest part :D to check how much he have x gold and send it x amount point i know how to check if he have 10gold and give 1k point if he does but then how to do calculations if he have like 60 gold or 500gold it should give 6k points or 50k points you know like automatic calculation. You know what i mean
First option
Create few items, one that gives 1000 points, one for 5000, one for 10000 etc and set adequate price at npc.

Second option
Make the npc ask how much gold player wants to exchange to points first. Save it in variable and check if player:removeMoney.
 
First option
Create few items, one that gives 1000 points, one for 5000, one for 10000 etc and set adequate price at npc.

Second option
Make the npc ask how much gold player wants to exchange to points first. Save it in variable and check if player:removeMoney.
First option is something that isnt advanced and is low level solution because he can have any amount of gold so imagine adding from 10gold to a like 10k gold in config, thats actually stupid
First option
Create few items, one that gives 1000 points, one for 5000, one for 10000 etc and set adequate price at npc.

Second option
Make the npc ask how much gold player wants to exchange to points first. Save it in variable and check if player:removeMoney.
Have no idea about second option how to make it, thats what i asked in my post because i knew that you can make something like this, but have no clue and you posted exact same stuff i had in mind, not sure how it helped in any way :D Its like saying to person how to build a house "-Just build foundations first, then build the house" thats what you basically did :D
 
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local amount = {}

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

npcHandler:addModule(FocusModule:new())

local function greetCallback(cid)
    amount[cid] = nil
    return true
end

local function isValidPointsAmount(points)
    if isNumber(points) == TRUE and points > 0 and points < 999999999 then
        return TRUE
    end
    return FALSE
end


local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
   
    if msgcontains(msg, 'premium points') then
        if player:getLevel() < 150 then
            npcHandler:say('You need 150 level to exchange gold for points.', cid)
            npcHandler.topic[cid] = 0
        end
        if string.match(msg,'%d+') then
            amount[cid] = math.max(1, getCount(msg))
            if amount[cid] < 100 then
                npcHandler:say('You cannot buy less than 100 premium points.', cid)
                npcHandler.topic[cid] = 0
                return false
            end
            npcHandler:say('Would you like to buy ' .. amount[cid] .. ' premium points for ' .. amount[cid] / 10 .. ' gold coins?', cid)
            npcHandler.topic[cid] = 2
            return true
        else
            npcHandler:say('Please tell me how many premium points you would like to buy.', cid)
            npcHandler.topic[cid] = 1
            return true
        end
    elseif npcHandler.topic[cid] == 1 then
            amount[cid] = math.max(1, getCount(msg))
            if amount[cid] < 100 then
                npcHandler:say('You cannot buy less than 100 premium points.', cid)
                npcHandler.topic[cid] = 0
                return false
            end
            if isValidPointsAmount(amount[cid]) then
                npcHandler:say('Would you like to buy ' .. amount[cid] .. ' premium points for ' .. amount[cid] / 10 .. ' gold coins?', cid)
                npcHandler.topic[cid] = 2
                return true
            end
    elseif msgcontains(msg, 'yes') then
        if npcHandler.topic[cid] == 2 then
            if player:getMoney() >= tonumber(amount[cid] / 10) then
                player:removeMoney(amount[cid] / 10)
                db.query('UPDATE accounts SET premium_points = premium_points+'.. amount[cid] ..' WHERE id = ' .. getAccountNumberByPlayerName(getCreatureName(cid)))
                npcHandler:say('Alright, I have added the amount of ' .. amount[cid] .. ' premium points to your account.', cid)
            else
                npcHandler:say('You do not have enough gold.', cid)
            end
            npcHandler.topic[cid] = 0
        end
    end
end
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)

Here you are. Tested on 1.x.
 
Last edited:
Solution
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local amount = {}

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

npcHandler:addModule(FocusModule:new())

local function greetCallback(cid)
    amount[cid] = nil
    return true
end

local function isValidPointsAmount(points)
    if isNumber(points) == TRUE and points > 0 and points < 999999999 then
        return TRUE
    end
    return FALSE
end


local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
  
    if msgcontains(msg, 'premium points') then
        if player:getLevel() < 150 then
            npcHandler:say('You need 150 level to exchange gold for points.', cid)
            npcHandler.topic[cid] = 0
        end
        if string.match(msg,'%d+') then
            amount[cid] = math.max(1, getCount(msg))
            if amount[cid] < 100 then
                npcHandler:say('You cannot buy less than 100 premium points.', cid)
                npcHandler.topic[cid] = 0
                return false
            end
            npcHandler:say('Would you like to buy ' .. amount[cid] .. ' premium points for ' .. amount[cid] / 10 .. ' gold coins?', cid)
            npcHandler.topic[cid] = 2
            return true
        else
            npcHandler:say('Please tell me how many premium points you would like to buy.', cid)
            npcHandler.topic[cid] = 1
            return true
        end
    elseif npcHandler.topic[cid] == 1 then
            amount[cid] = math.max(1, getCount(msg))
            if amount[cid] < 100 then
                npcHandler:say('You cannot buy less than 100 premium points.', cid)
                npcHandler.topic[cid] = 0
                return false
            end
            if isValidPointsAmount(amount[cid]) then
                npcHandler:say('Would you like to buy ' .. amount[cid] .. ' premium points for ' .. amount[cid] / 10 .. ' gold coins?', cid)
                npcHandler.topic[cid] = 2
                return true
            end
    elseif msgcontains(msg, 'yes') then
        if npcHandler.topic[cid] == 2 then
            if msgcontains(msg, 'yes') then
                if player:getMoney() >= tonumber(amount[cid] / 10) then
                    player:removeMoney(amount[cid] / 10)
                    db.query('UPDATE accounts SET premium_points = premium_points+'.. amount[cid] ..' WHERE id = ' .. getAccountNumberByPlayerName(getCreatureName(cid)))
                    npcHandler:say('Alright, I have added the amount of ' .. amount[cid] .. ' premium points to your account.', cid)
                else
                    npcHandler:say('You do not have enough gold.', cid)
                end
                npcHandler.topic[cid] = 0
            end
        end
    end
end
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)

Here you are. Tested on 1.x.
 
Does the player have to do 10 gold increments? Can the player do like 15 gold for 1500 points?
Not really because probably later i will edit this code for premium days too not only for premium points so then this code wont be that customizable
 
Thought that isNumber function is by default on TFS 1.x (I might be wrong as I have tested in on some datapack I had on my machine), just add it somewhere in your global.lua or to your NPC file.
LUA:
function isNumber(str)
    return tonumber(str) ~= nil
end

Note, in this code 100 premium points equals 10 gold coins and so on, just as you asked in this thread but it's super easy to modify it as you wish in the future.
 
Last edited:
Thought that isNumber function is by default on TFS 1.x (I might be wrong as I have tested in on some datapack I had on my machine), just add it somewhere in your global.lua or to your NPC file.
LUA:
function isNumber(str)
    return tonumber(str) ~= nil
end

Note, in this code 100 premium points equals 10 gold coins and so on, just as you asked in this thread but it's super easy to modify it as you wish in the future.
Your code could have been more advanced with local config :D But w/e. Btw cant figure out how to make 1 premium point = 1000 gold. What i get is 0.001 gold value :D
 
Your code could have been more advanced with local config :D But w/e. Btw cant figure out how to make 1 premium point = 1000 gold. What i get is 0.001 gold value :D
Tfs 1.x
Need npc that trades 10gold to 1000 premium points so if you have like 40gold you would get 4000 premium points and etc. And level requirement min 150lvl
I have made exactly what you have asked, 10 gold for 1000 points and so on what kind of more advanced local config you want? Anyway I have added one more config value which is premiumPointsPrice (cost of one premium point)

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

local amount = {}
local premiumPointsPrice = 10 -- cost of one premium point

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

npcHandler:addModule(FocusModule:new())

local function greetCallback(cid)
    amount[cid] = nil
    return true
end

local function isValidPointsAmount(points)
    if isNumber(points) == TRUE and points > 0 and points < 999999999 then
        return TRUE
    end
    return FALSE
end


local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)

    if msgcontains(msg, 'premium points') then
        if player:getLevel() < 150 then
            npcHandler:say('You need 150 level to exchange gold for points.', cid)
            npcHandler.topic[cid] = 0
        end
        if string.match(msg,'%d+') then
            amount[cid] = math.max(1, getCount(msg))
            if amount[cid] < 100 then
                npcHandler:say('You cannot buy less than 100 premium points.', cid)
                npcHandler.topic[cid] = 0
                return false
            end
            npcHandler:say('Would you like to buy ' .. amount[cid] .. ' premium points for ' .. amount[cid] * premiumPointsPrice .. ' gold coins?', cid)
            npcHandler.topic[cid] = 2
            return true
        else
            npcHandler:say('Please tell me how many premium points you would like to buy.', cid)
            npcHandler.topic[cid] = 1
            return true
        end
    elseif npcHandler.topic[cid] == 1 then
            amount[cid] = math.max(1, getCount(msg))
            if amount[cid] < 100 then
                npcHandler:say('You cannot buy less than 100 premium points.', cid)
                npcHandler.topic[cid] = 0
                return false
            end
            if isValidPointsAmount(amount[cid]) then
                npcHandler:say('Would you like to buy ' .. amount[cid] .. ' premium points for ' .. amount[cid] * premiumPointsPrice .. ' gold coins?', cid)
                npcHandler.topic[cid] = 2
                return true
            end
    elseif msgcontains(msg, 'yes') then
        if npcHandler.topic[cid] == 2 then
            if player:getMoney() >= tonumber(amount[cid] * premiumPointsPrice) then
                player:removeMoney(amount[cid] * premiumPointsPrice)
                db.query('UPDATE accounts SET premium_points = premium_points+'.. amount[cid] ..' WHERE id = ' .. getAccountNumberByPlayerName(getCreatureName(cid)))
                npcHandler:say('Alright, I have added the amount of ' .. amount[cid] .. ' premium points to your account.', cid)
            else
                npcHandler:say('You do not have enough gold.', cid)
            end
            npcHandler.topic[cid] = 0
        end
    end
end
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
 
Last edited:
Back
Top