• 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 0.3.6 Name: Buy & Sell Talkaction System

samuel157

/root
Joined
Mar 19, 2010
Messages
518
Solutions
3
Reaction score
71
Location
São Paulo, Brazil
GitHub
Samuel10M
BUY .LUA

LUA:
--[[ Buy & Sell Talkaction System

Made by Yohan(me). ]]--

function onSay(cid, words, param) -- script by Yohan

local config = {

              levelNeeded = 8,

              muteTime = 120, -- time in Seconds that the player will be without broadcasting.

              storage = 7896 -- storage that controls the broadcasting of the player.

              }

if param == '' then

doPlayerPopupFYI(cid, "Say '!buy Item Name, Price in GP'")

end



t = string.explode(param, ",")

if not(t[1]) or not(t[2]) then

doPlayerSendCancel(cid, "Command requires more than one parameter.")

else



if getPlayerLevel(cid) >= config.levelNeeded then



  if getPlayerStorageValue(cid,config.storage) == -1 then

  setPlayerStorageValue(cid, config.storage, os.time())



  elseif getPlayerStorageValue(cid,config.storage) > os.time() then

  doPlayerSendCancel(cid, "You can only place one offer in " .. config.muteTime .. " seconds.")



  elseif getPlayerStorageValue(cid,config.storage) <= os.time() then

  doBroadcastMessage("Player " .. getPlayerName(cid) .. " is buying " .. t[1] .. " for " .. t[2] .. " gold coins.")

  setPlayerStorageValue(cid,config.storage, (os.time() + config.muteTime))

  end



else

doPlayerSendCancel(cid, "Only players with level " .. config.levelNeeded .. "+ can broadcast one offer.")

end

end



return true

end

SELL .LUA


LUA:
--[[ Buy & Sell Talkaction System
Made by Yohan(me). ]]--
function onSay(cid, words, param) -- script by Yohan
local config = {
              levelNeeded = 8,
              muteTime = 120, -- time in Seconds that the player will be without broadcasting.
              storage = 7896 -- storage that controls the broadcasting of the player.
              }
if param == '' then
doPlayerPopupFYI(cid, "Say '!sell Item Name, Price in GP'")
end

t = string.explode(param, ",")
if not(t[1]) or not(t[2]) then
doPlayerSendCancel(cid, "Command requires more than one parameter.")
else

if getPlayerLevel(cid) >= config.levelNeeded then

  if getPlayerStorageValue(cid,config.storage) == -1 then
  setPlayerStorageValue(cid, config.storage, os.time())

  elseif getPlayerStorageValue(cid,config.storage) > os.time() then
  doPlayerSendCancel(cid, "You can only place one offer in " .. config.muteTime .. " seconds.")

  elseif getPlayerStorageValue(cid,config.storage) <= os.time() then
  doBroadcastMessage("Player " .. getPlayerName(cid) .. " is selling " .. t[1] .. " for " .. t[2] .. " gold coins.")
  setPlayerStorageValue(cid,config.storage, (os.time() + config.muteTime))
  end

else
doPlayerSendCancel(cid, "Only players with level " .. config.levelNeeded .. "+ can broadcast one offer.")
end
end

return true
end

validCurrencies = {9971, 2160, 2148, 2152, 2159}, maxPrice = 1000000000
 
OTLAND HELP ME !SELL AND !LIST FUNCTION ONLY BUY NO FUNCTION

LUA:
function onSay(cid, words, param)
    if words == "!sell" then
        -- Código de venda (não alterado)
    end

    if words == "!buy" then
        if param == "" then
            doPlayerSendCancel(cid, "Use: !buy itemName, quantity, totalPrice, currencyId, sellerName")
            return true
        end

        -- Separar os parâmetros fornecidos
        local t = string.explode(param, ",")
        if #t ~= 5 then
            doPlayerSendCancel(cid, "Invalid parameters. Use: !buy itemName, quantity, totalPrice, currencyId, sellerName")
            return true
        end

        local itemName = t[1]:trim()
        local quantity = tonumber(t[2])
        local totalPrice = tonumber(t[3])
        local currencyId = tonumber(t[4])
        local sellerName = t[5]:trim()

        -- Validar os parâmetros
        if not quantity or quantity <= 0 then
            doPlayerSendCancel(cid, "Invalid quantity.")
            return true
        end

        if not totalPrice or totalPrice <= 0 or totalPrice > config.maxPrice then
            doPlayerSendCancel(cid, "Invalid price. Maximum allowed: " .. config.maxPrice)
            return true
        end

        if not isValidCurrency(currencyId) then
            doPlayerSendCancel(cid, "Invalid currency ID.")
            return true
        end

        -- Procurar pelo item à venda
        for i, sell in ipairs(activeSells) do
            -- Verificar se o item, a quantidade, o preço e o vendedor batem
            if sell.itemName == itemName and sell.quantity >= quantity and sell.seller == sellerName then
                local pricePerUnit = sell.price * quantity  -- Preço total pela quantidade
                if pricePerUnit ~= totalPrice then
                    doPlayerSendCancel(cid, "The total price does not match the quantity and price per unit.")
                    return true
                end

                -- Verificar se o comprador tem moedas suficientes
                if getPlayerItemCount(cid, currencyId) < totalPrice then
                    doPlayerSendCancel(cid, "You don't have enough " .. getItemNameById(currencyId) .. " to complete this purchase.")
                    return true
                end

                -- Realizar a transação
                doPlayerRemoveItem(cid, currencyId, totalPrice) -- Remover as moedas do comprador
                doPlayerAddItem(cid, getItemIdByName(itemName), quantity) -- Adicionar o item ao comprador

                -- Notificar o vendedor
                local seller = getPlayerByName(sell.seller)
                if seller then
                    doPlayerAddItem(seller, currencyId, totalPrice) -- Transferir as moedas para o vendedor
                    doPlayerSendTextMessage(seller, MESSAGE_INFO_DESCR, "Your item '" .. itemName .. "' (" .. quantity .. "x) has been sold for " .. totalPrice .. " " .. getItemNameById(currencyId) .. ".")
                end

                -- Atualizar ou remover o item da venda
                sell.quantity = sell.quantity - quantity
                if sell.quantity <= 0 then
                    table.remove(activeSells, i)
                end

                doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You bought " .. quantity .. "x " .. itemName .. " for " .. totalPrice .. " " .. getItemNameById(currencyId) .. ".")
                return true
            end
        end

        doPlayerSendCancel(cid, "The item was not found or is unavailable.")
        return true
    end

    return false
end

LUA:
-- Inicializar a tabela global para vendas ativas
activeSells = activeSells or {}

-- Configuração
local config = {
    validCurrencies = {9971, 2160, 2148, 2152, 2159}, -- Moedas válidas
    maxPrice = 1000000000, -- Preço máximo permitido
}

-- Verificar se uma moeda é válida
local function isValidCurrency(currencyId)
    for _, validId in ipairs(config.validCurrencies) do
        if validId == currencyId then
            return true
        end
    end
    return false
end

function onSay(cid, words, param)
    if param == "" then
        doPlayerSendCancel(cid, "Use: !sell itemName, quantity, price, currencyId")
        return true
    end

    local t = string.explode(param, ",")
    if #t ~= 4 then
        doPlayerSendCancel(cid, "Invalid parameters. Use: !sell itemName, quantity, price, currencyId")
        return true
    end

    local itemName = t[1]:trim()
    local quantity = tonumber(t[2])
    local price = tonumber(t[3])
    local currencyId = tonumber(t[4])

    if not quantity or quantity <= 0 then
        doPlayerSendCancel(cid, "Invalid quantity.")
        return true
    end

    if not price or price <= 0 or price > config.maxPrice then
        doPlayerSendCancel(cid, "Invalid price. Maximum allowed: " .. config.maxPrice)
        return true
    end

    if not isValidCurrency(currencyId) then
        doPlayerSendCancel(cid, "Invalid currency. Valid currencies: " .. table.concat(config.validCurrencies, ", "))
        return true
    end

    -- Verificar se o jogador tem o item suficiente no inventário
    local itemId = getItemIdByName(itemName)
    if itemId == 0 or getPlayerItemCount(cid, itemId) < quantity then
        doPlayerSendCancel(cid, "You don't have enough of the item '" .. itemName .. "' to sell.")
        return true
    end

    -- Remover os itens do inventário do vendedor
    doPlayerRemoveItem(cid, itemId, quantity)

    -- Registrar a venda
    table.insert(activeSells, {
        itemName = itemName,
        quantity = quantity,
        price = price,
        currencyId = currencyId,
        seller = getPlayerName(cid),
    })

    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Your item '" .. itemName .. "' has been listed for sale.")
    return true
end

LUA:
-- Inicializar activeSells como tabela, se ainda não foi
activeSells = activeSells or {}

function onSay(cid, words, param)
    if #activeSells == 0 then
        doPlayerSendCancel(cid, "No items are currently being sold.")
        return true
    end

    local message = "Items available for sale:\n"
    for i, sell in ipairs(activeSells) do
        message = message .. i .. ". " .. sell.itemName .. " (" .. sell.quantity .. "x) - Price: " .. sell.price .. " gold coins - Seller: " .. sell.seller .. "\n"
    end

    doPlayerPopupFYI(cid, message)
    return true
end
 
Back
Top