• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

NPC [8.21]Dark rodo with new system buy/sell

Sibuna

New Member
Joined
Feb 1, 2008
Messages
7
Reaction score
1
Code:
<npc name="Dark rodo" script="runes.lua" autowalk="0">
    <health now="150" max="150"/>
    <look type="289" head="140" body="64" legs="121" feet="76" addons="3" corpse="2212"/>
</npc>
Code:
local focuses = {}
local function isFocused(cid)
    for i, v in pairs(focuses) do
        if(v == cid) then
            return true
        end
    end
    return false
end

local function addFocus(cid)
    if(not isFocused(cid)) then
        table.insert(focuses, cid)
    end
end
local function removeFocus(cid)
    for i, v in pairs(focuses) do
        if(v == cid) then
            table.remove(focuses, i)
            break
        end
    end
end
local function lookAtFocus()
    for i, v in pairs(focuses) do
        if(isPlayer(v) == TRUE) then
            doNpcSetCreatureFocus(v)
            return
        end
    end
    doNpcSetCreatureFocus(0)
end

local itemWindow = {
[COLOR=#ff0000]    {id=2268, subtype=0, buy=215, sell=0},
    {id=2273, subtype=0, buy=100, sell=0},
    {id=2304, subtype=0, buy=125, sell=0},
    {id=2274, subtype=0, buy=10000, sell=0}[/COLOR]
}

local items = {}
for _, item in ipairs(itemWindow) do
    items[item.id] = {buyPrice = item.buy, sellPrice = item.sell, subtype = item.subtype}
end

local function getPlayerMoney(cid)
    return ((getPlayerItemCount(cid, 2160) * 10000) + 
    (getPlayerItemCount(cid, 2152) * 100) + 
    getPlayerItemCount(cid, 2148))
end

local onBuy = function(cid, item, subtype, amount)
    if(items[item] == nil) then
        selfSay("Ehm.. sorry... this shouldn't be there, I'm not selling it.", cid)
        return
    end

    if(getPlayerMoney(cid) >= amount*items[item].buyPrice) then
        local itemz, i = doPlayerAddItem(cid, item, subtype, amount)
        if(i < amount) then
            if(i == 0) then
                selfSay("Sorry, but you don't have space to take it.", cid)
            else
                selfSay("I've sold some for you, but it seems you can't carry more than this. I won't take more money than necessary.", cid)
                doPlayerRemoveMoney(cid, i*items[item].buyPrice)
            end
        else
            selfSay("Thanks for the money!", cid)
            doPlayerRemoveMoney(cid, amount*items[item].buyPrice)
        end
    else
        selfSay("You don't have money.", cid)
    end
end

local onSell = function(cid, item, subtype, amount)
    if(items[item] == nil) then
        selfSay("Ehm.. sorry... this shouldn't be there, I'm not buying it.", cid)
    end

    if(subtype < 1) then
        subtype = -1
    end
    if(doPlayerRemoveItem(cid, item, amount, subtype) == TRUE) then
        doPlayerAddMoney(cid, items[item].sellPrice*amount)
        selfSay("Here you are.", cid)
    else
        selfSay("No item, no deal.", cid)
    end
end

function onCreatureAppear(cid)
end

function onCreatureDisappear(cid)
    if(isFocused(cid)) then
        selfSay("Hmph!")
        removeFocus(cid)
        if(isPlayer(cid) == TRUE) then --Be sure he's online
            closeShopWindow(cid)
        end
    end
end

function onCreatureSay(cid, type, msg)
    if((msg == "hi") and not (isFocused(cid))) then
        selfSay("Welcome, ".. getCreatureName(cid) ..".", cid, TRUE)
        selfSay([COLOR=#ff0000]"I sell all types {runes}?[/COLOR]", cid)
        addFocus(cid)
    elseif((isFocused(cid)) and (msg == "runes" or msg == "trade")) then
        selfSay("[COLOR=Red]Pretty nice, right?[/COLOR]", cid)
        openShopWindow(cid, itemWindow, onBuy, onSell)
    elseif((isFocused(cid)) and (msg == "bye" or msg == "goodbye" or msg == "cya")) then
        selfSay("Goodbye!", cid, TRUE)
        closeShopWindow(cid)
        removeFocus(cid)
    end
end

function onPlayerCloseChannel(cid)
    if(isFocused(cid)) then
        selfSay("Hmph!")
        closeShopWindow(cid)
        removeFocus(cid)
    end
end

function onPlayerEndTrade(cid)
    selfSay("It was a pleasure doing business with you.", cid)
end

function onThink()
    for i, focus in pairs(focuses) do
        if(isCreature(focus) == FALSE) then
            removeFocus(focus)
        else
            local distance = getDistanceTo(focus) or -1
            if((distance > 4) or (distance == -1)) then
                selfSay("Hmph!")
                closeShopWindow(focus)
                removeFocus(focus)
            end
        end
    end
    lookAtFocus()
end
 
Last edited by a moderator:
Hi
Do you know why I'm getting this error? :
Code:
Lua script error: [Npc interface]
(Unknown scriptfile)

data/npc/scripts/dark.lua:72: attempt to compare nil with number
stack tracaback:
	data/npc/scripts/dark.lua:72: in function <data/npc/scripts/dark.lua:63>

I can sell item but I can't buy. I mean I can get item but every item is for free :/ and this error
 
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Dark Rodo" script="data/npc/scripts/runes1.lua" walkinterval="2000" floorchange="0">
<health now="100" max="100"/>
<look type="289" head="140" body="64" legs="121" feet="76" addons="3"/>
</npc>
Code:
local focuses = {}
local function isFocused(cid)
for i, v in pairs(focuses) do
if(v == cid) then
return true
end
end
return false
end

local function addFocus(cid)
if(not isFocused(cid)) then
table.insert(focuses, cid)
end
end
local function removeFocus(cid)
for i, v in pairs(focuses) do
if(v == cid) then
table.remove(focuses, i)
break
end
end
end
local function lookAtFocus()
for i, v in pairs(focuses) do
if(isPlayer(v) == TRUE) then
doNpcSetCreatureFocus(v)
return
end
end
doNpcSetCreatureFocus(0)
end

local itemWindow = {
{id=2268, subtype=0, buy=215, sell=0},
{id=2273, subtype=0, buy=100, sell=0},
{id=2313, subtype=0, buy=125, sell=0},
{id=2304, subtype=0, buy=125, sell=0},
{id=8828, subtype=0, buy=22000, sell=0},
{id=2187, subtype=0, buy=15000, sell=0},
{id=2188, subtype=0, buy=5000, sell=0},
{id=8827, subtype=0, buy=7500, sell=0},
{id=8826, subtype=0, buy=18000, sell=0},
{id=2189, subtype=0, buy=10000, sell=0},
{id=2190, subtype=0, buy=500, sell=0},
{id=2191, subtype=0, buy=1000, sell=0},
{id=2274, subtype=0, buy=100, sell=0},
{id=8817, subtype=0, buy=750, sell=0},
{id=8816, subtype=0, buy=22000, sell=0},
{id=2183, subtype=0, buy=15000, sell=0},
{id=2181, subtype=0, buy=10000, sell=0},
{id=2182, subtype=0, buy=500, sell=0},
{id=2185, subtype=0, buy=5000, sell=0},
{id=2186, subtype=0, buy=1000, sell=0},
{id=8818, subtype=0, buy=18000, sell=0},
{id=8377, subtype=0, buy=310, sell=0},
{id=7591, subtype=0, buy=190, sell=0},
{id=7588, subtype=0, buy=100, sell=0},
{id=7618, subtype=0, buy=45, sell=0},
{id=8376, subtype=0, buy=190, sell=0},
{id=7590, subtype=0, buy=120, sell=0},
{id=7589, subtype=0, buy=80, sell=0},
{id=7620, subtype=0, buy=50, sell=0},
{id=2163, subtype=0, buy=500, sell=0},
{id=2311, subtype=0, buy=300, sell=0},
{id=2260, subtype=0, buy=10, sell=0}
}

local items = {}
for _, item in ipairs(itemWindow) do
items[item.id] = {buyPrice = item.buy, sellPrice = item.sell, subtype = item.subtype}
end

local function getPlayerMoney(cid)
return ((getPlayerItemCount(cid, 2160) * 10000) + 
(getPlayerItemCount(cid, 2152) * 100) + 
getPlayerItemCount(cid, 2148))
end

local onBuy = function(cid, item, subtype, amount)
if(items[item] == nil) then
selfSay("Ehm.. sorry... this shouldn't be there, I'm not selling it.", cid)
return
end

if(getPlayerMoney(cid) >= amount*items[item].buyPrice) then
local itemz, i = doPlayerAddItem(cid, item, subtype, amount)
if(i < amount) then
if(i == 0) then
selfSay("Sorry, but you don't have space to take it.", cid)
else
selfSay("I've sold some for you, but it seems you can't carry more than this. I won't take more money than necessary.", cid)
doPlayerRemoveMoney(cid, i*items[item].buyPrice)
end
else
selfSay("Thanks for the money!", cid)
doPlayerRemoveMoney(cid, amount*items[item].buyPrice)
end
else
selfSay("You don't have money.", cid)
end
end

local onSell = function(cid, item, subtype, amount)
if(items[item] == nil) then
selfSay("Ehm.. sorry... this shouldn't be there, I'm not buying it.", cid)
end

if(subtype < 1) then
subtype = -1
end
if(doPlayerRemoveItem(cid, item, amount, subtype) == TRUE) then
doPlayerAddMoney(cid, items[item].sellPrice*amount)
selfSay("Here you are.", cid)
else
selfSay("No item, no deal.", cid)
end
end

function onCreatureAppear(cid)
end

function onCreatureDisappear(cid)
if(isFocused(cid)) then
selfSay("Hmph!")
removeFocus(cid)
if(isPlayer(cid) == TRUE) then --Be sure he's online
closeShopWindow(cid)
end
end
end

function onCreatureSay(cid, type, msg)
if((msg == "hi") and not (isFocused(cid))) then
selfSay("Welcome, ".. getCreatureName(cid) ..".", cid, TRUE)
selfSay("I sell all types {runes}?", cid)
addFocus(cid)
elseif((isFocused(cid)) and (msg == "runes" or msg == "trade")) then
selfSay("Pretty nice, right?", cid)
openShopWindow(cid, itemWindow, onBuy, onSell)
elseif((isFocused(cid)) and (msg == "bye" or msg == "goodbye" or msg == "cya")) then
selfSay("Goodbye!", cid, TRUE)
closeShopWindow(cid)
removeFocus(cid)
end
end

function onPlayerCloseChannel(cid)
if(isFocused(cid)) then
selfSay("Hmph!")
closeShopWindow(cid)
removeFocus(cid)
end
end

function onPlayerEndTrade(cid)
selfSay("It was a pleasure doing business with you.", cid)
end

function onThink()
for i, focus in pairs(focuses) do
if(isCreature(focus) == FALSE) then
removeFocus(focus)
else
local distance = getDistanceTo(focus) or -1
if((distance > 4) or (distance == -1)) then
selfSay("Hmph!")
closeShopWindow(focus)
removeFocus(focus)
end
end
end
lookAtFocus()
end
I have edited for TFS 8.21 (2.0)
 
Last edited by a moderator:
still same bug :( I copied your code, now it says line 80 is bugged (if(i < amount) then)
 
Last edited:
Code:
<npc name="Dark rodo" script="runes.lua" autowalk="0">
    <health now="150" max="150"/>
    <look type="289" head="140" body="64" legs="121" feet="76" addons="3" corpse="2212"/>
<parameter key="module_shop" value="2" />
<parameter key="shop_sellable" value="plate legs,2647,135" />
</npc>
 
Back
Top