• 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 Exchange Npc

3li Xiber

Active Member
Joined
Dec 29, 2014
Messages
395
Reaction score
31
tfs 0.3.6
its Npc Who Take items and give points.
I use script from @Jotran and i need edit it
so every account can make only 4 changes from the npc
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local t = {}
-- this will allow you to use 1 item or 100 or more items, its good to have flexible code :)
local item = {
    {id = 6527, count = 150}, -- 1st item
    {id = 6527, count = 150} -- 2nd item
}


local points = 5 -- amount of premium points

function removeItems(cid, item)
    if type(item) == "table" then -- make sure item is a table
        for index = 1, #item do
            if getPlayerItemCount(cid, item[index].id) == item[index].count then
            -- do nothing, we want to make sure the player has all the items needed before we remove them
            else
                return false -- if they don't have the required amount then return false
            end
        end
        for i = 1, #item do -- now remove them
            doPlayerRemoveItem(cid, item[i].id, item[i].count)
        end
        return true -- return true if all the items have been removed
    end
end

function getItemsCountAndNames(item)
    local names = {}
    for i = 1, #item do
        names[i] = item[i].count.." of ".. getItemNameById(item[i].id).." "
    end
    return table.concat(names)
end

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, 'change') then

        selfSay('are you sure you want to change '..getItemsCountAndNames(item) ..'for '.. points ..' premium points?', cid)
        t[cid] = 1
    elseif t[cid] == 1 then
        npcHandler:releaseFocus(cid)
        t[cid] = nil
        if msgcontains(msg, 'yes') then
            if removeItems(cid, item) then
                local p = "UPDATE `accounts` SET `premium_points` = `premium_points` + "..points.." where id="..getPlayerAccountId(cid)
                db.executeQuery(p)
                selfSay('you have recieved '.. points ..' premium points', cid)
            else
                selfSay('you don\'t have enough coins you need '.. getItemsCountAndNames(item) ..'to recieve '.. points ..' premium points', cid)
            end
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Sorry I still don't know what you mean, this npc sells premium points in exchange for 2 sets of items, so what are you talking about when you say change?

Don't just keep saying "change" your not making any sense at least not in english.
 
look,
i use "Change" To Buy Premium Points for 2 sets of items in my ot.

i need every player can buy premium points from npc only 4 times per account
if u understand me tell me xD
 
Updated
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local t = {}
-- this will allow you to use 1 item or 100 or more items, its good to have flexible code :)
local item = {
    {id = 6527, count = 150}, -- 1st item
    {id = 6527, count = 150} -- 2nd item
}

local exchange = {
    ["storage"] = 1000, -- change this to a storage value that isn't in use
    ["limit"] = 4 -- max number of times a exchange can be made
}

local points = 5 -- amount of premium points

function removeItems(cid, item)
    if type(item) == "table" then -- make sure item is a table
        for index = 1, #item do
            if getPlayerItemCount(cid, item[index].id) == item[index].count then
            -- do nothing, we want to make sure the player has all the items needed before we remove them
            else
                return false -- if they don't have the required amount then return false
            end
        end
        for i = 1, #item do -- now remove them
            doPlayerRemoveItem(cid, item[i].id, item[i].count)
        end
        return true -- return true if all the items have been removed
    end
end

function getItemsCountAndNames(item)
    local names = {}
    for i = 1, #item do
        names[i] = item[i].count.." of ".. getItemNameById(item[i].id).." "
    end
    return table.concat(names)
end

function getLimit(cid, exchange)
    return getPlayerStorageValue(cid, exchange["storage"]) <= exchange["limit"]
end

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, 'change') then

        selfSay('are you sure you want to change '..getItemsCountAndNames(item) ..'for '.. points ..' premium points?', cid)
        t[cid] = 1
    elseif t[cid] == 1 then
        npcHandler:releaseFocus(cid)
        t[cid] = nil
        if msgcontains(msg, 'yes') then
            if getLimit(cid, exchange) then
                if removeItems(cid, item) then
                    local p = "UPDATE `accounts` SET `premium_points` = `premium_points` + "..points.." where id="..getPlayerAccountId(cid)
                    db.executeQuery(p)
                    setPlayerStorageValue(cid, exchange["storage"], (getPlayerStorageValue(cid, exchange["storage"]) < 1) and 1 or getPlayerStorageValue(cid, exchange["storage"]) + 1 )
                    selfSay('you have recieved '.. points ..' premium points', cid)
                else
                    selfSay('you don\'t have enough coins you need '.. getItemsCountAndNames(item) ..'to recieve '.. points ..' premium points', cid)
                end
            else
                selfSay('sorry, your account has reached the maximum amount of times it can exchange items for premium points :(', cid)
            end
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Last edited:
You might need to create a new column in player in the database to accomplish this or maybe someone knows another way, I can't really help you any further because I don't have this distro in front of me :(
 
Actually this code won't work for an entire account, but I will leave it in case someone else will find some use for it.
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local t = {}
-- this will allow you to use 1 item or 100 or more items, its good to have flexible code :)
local item = {
    {id = 6527, count = 150}, -- 1st item
    {id = 6527, count = 150} -- 2nd item
}

local exchange = {
    ["storage"] = 1000, -- change this to a storage value that isn't in use
    ["limit"] = 4 -- max number of times a exchange can be made
}

local points = 5 -- amount of premium points

function removeItems(cid, item)
    if type(item) == "table" then -- make sure item is a table
        for index = 1, #item do
            if getPlayerItemCount(cid, item[index].id) == item[index].count then
            -- do nothing, we want to make sure the player has all the items needed before we remove them
            else
                return false -- if they don't have the required amount then return false
            end
        end
        for i = 1, #item do -- now remove them
            doPlayerRemoveItem(cid, item[i].id, item[i].count)
        end
        return true -- return true if all the items have been removed
    end
end

function getItemsCountAndNames(item)
    local names = {}
    for i = 1, #item do
        names[i] = item[i].count.." of ".. getItemNameById(item[i].id).." "
    end
    return table.concat(names)
end

function getLimit(cid, exchange)
    return getPlayerStorageValue(cid, exchange["storage"]) <= exchange["limit"]
end

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, 'change') then

        selfSay('are you sure you want to change '..getItemsCountAndNames(item) ..'for '.. points ..' premium points?', cid)
        t[cid] = 1
    elseif t[cid] == 1 then
        npcHandler:releaseFocus(cid)
        t[cid] = nil
        if msgcontains(msg, 'yes') then
            if getLimit(cid, exchange) then
                if removeItems(cid, item) then
                    local p = "UPDATE `accounts` SET `premium_points` = `premium_points` + "..points.." where id="..getPlayerAccountId(cid)
                    db.executeQuery(p)
                    setPlayerStorageValue(cid, exchange["storage"], (exchange["storage"] < 0) and 1 or exchange["storage"] + 1 )
                    selfSay('you have recieved '.. points ..' premium points', cid)
                else
                    selfSay('you don\'t have enough coins you need '.. getItemsCountAndNames(item) ..'to recieve '.. points ..' premium points', cid)
                end
            else
                selfSay('sorry, your account has reached the maximum amount of times it can exchange items for premium points :(', cid)
            end
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
this script works good
but i can buy premium points (1) time only not (4)
If i created another char same account i can buy premium point too
 
this script works good
but i can buy premium points (1) time only not (4)
Because it written incorrectly I didn't fix it because even if i did it would only effect 1 character on the account so no point in fixing the code.
If i created another char same account i can buy premium point too
I already stated it only would work for the character on the account but not the whole account
 
It's been fixed, this is the last script I will write or modify for anyone on otland.

Do yourself a favor and learn how to program, your not going to learn how to program overnight but if you put in the effort you will learn overtime and all these scripts that look like chinese will begin to look like your native tongue.
 
Last edited:
You should learn lua and fix it yourself :)
It is a small problem.. sorry but I won't help you anymore because you aren't even trying to learn anything from the script, all you know is support or request is for free scripts for your shit server :D
hey girl stop say that i no try but i don't know the steps to create npc xml to do it i can edit small things but not to put new thing
 
and i don't use support or request manytimes lol . ....
i think my all threads were on ( Team Battle Event - Rush Event - Point Exchanger )
 
ok jotran thx for your help i will try to do it
i think
Code:
setPlayerStorageValue(cid, exchange["storage"], (getPlayerStorageValue(cid, exchange["storage"]) < 1) and 1 or getPlayerStorageValue(cid, exchange["storage"]) + 1 )
must be +4 :cool:
 
Back
Top