samuel157
/root
- Joined
- Mar 19, 2010
- Messages
- 518
- Solutions
- 3
- Reaction score
- 71
- Location
- São Paulo, Brazil
- GitHub
- Samuel10M
!buy demon armor, 3, 1, player name demo armor send only 3 demon armor !buy demon armor, 3, 1, player name
buy 3 demon armors for 1 vip coin at the price the player launches on the market as 3
@potinho
<talkaction words="!buy;!sell" event="script" value="buy.lua"/>
<talkaction words="!list" event="script" value="checkselllist.lua"/>
buy 3 demon armors for 1 vip coin at the price the player launches on the market as 3
@potinho
<talkaction words="!buy;!sell" event="script" value="buy.lua"/>
<talkaction words="!list" event="script" value="checkselllist.lua"/>
LUA:
-- Initialize global tables for active purchases and sales
activeBuys = activeBuys or {}
activeSells = activeSells or {}
-- Configurations
local config = {
validCurrencyId = 11192, -- VIP coin ID
}
-- Function to list available items
local function listItems(cid)
if #activeSells == 0 then
doPlayerSendCancel(cid, "There are no items for sale at the moment.")
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 .. " VIP Coins - Seller: " .. sell.seller .. "\n"
end
doPlayerPopupFYI(cid, message)
return true
end
-- Function to sell an item
local function sellItem(cid, itemName, quantity, price)
local itemId = getItemIdByName(itemName)
if not itemId or itemId == 0 then
doPlayerSendCancel(cid, "Item not found.")
return true
end
if getPlayerItemCount(cid, itemId) < quantity then
doPlayerSendCancel(cid, "You do not have the required quantity of items.")
return true
end
table.insert(activeSells, {
itemName = itemName,
quantity = quantity,
price = price,
seller = getPlayerName(cid)
})
doPlayerRemoveItem(cid, itemId, quantity)
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You listed " .. quantity .. "x " .. itemName .. " for sale at " .. price .. " VIP Coins.")
return true
end
-- Function to buy an item
local function buyItem(cid, itemName, quantity, price, sellerName)
for i, sell in ipairs(activeSells) do
if sell.itemName:lower() == itemName:lower() and sell.seller:lower() == sellerName:lower() and sell.price == price and sell.quantity >= quantity then
if getPlayerItemCount(cid, config.validCurrencyId) < price then
doPlayerSendCancel(cid, "You do not have enough VIP Coins to make this purchase.")
return true
end
doPlayerRemoveItem(cid, config.validCurrencyId, price) -- Remove VIP Coins from buyer
doPlayerAddItem(cid, getItemIdByName(itemName), quantity) -- Add the item to the buyer
-- Update the quantity of the item being sold
sell.quantity = sell.quantity - quantity
-- Notify buyer and seller
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You bought " .. quantity .. "x " .. itemName .. " from " .. sellerName .. " for " .. price .. " VIP Coins.")
local sellerPlayer = getPlayerByName(sellerName)
if sellerPlayer then
doPlayerSendTextMessage(sellerPlayer, MESSAGE_INFO_DESCR, "Your item " .. itemName .. " was sold to " .. getPlayerName(cid) .. ".")
end
-- Remove from the list if quantity reaches zero
if sell.quantity <= 0 then
table.remove(activeSells, i)
end
return true
end
end
doPlayerSendCancel(cid, "Item not found or invalid parameters.")
return true
end
-- Main function
function onSay(cid, words, param)
if words == "!sell" then
local t = string.explode(param, ",")
if #t ~= 3 then
doPlayerSendCancel(cid, "Correct usage: !sell itemName, quantity, price")
return true
end
local itemName = t[1]:trim()
local quantity = tonumber(t[2])
local price = tonumber(t[3])
if not quantity or not price or quantity <= 0 or price <= 0 then
doPlayerSendCancel(cid, "Invalid quantity or price.")
return true
end
return sellItem(cid, itemName, quantity, price)
elseif words == "!buy" then
local t = string.explode(param, ",")
if #t ~= 4 then
doPlayerSendCancel(cid, "Correct usage: !buy itemName, quantity, price, sellerName")
return true
end
local itemName = t[1]:trim()
local quantity = tonumber(t[2])
local price = tonumber(t[3])
local sellerName = t[4]:trim()
if not quantity or not price or quantity <= 0 or price <= 0 then
doPlayerSendCancel(cid, "Invalid quantity or price.")
return true
end
return buyItem(cid, itemName, quantity, price, sellerName)
elseif words == "!list" then
return listItems(cid)
end
return false
end
Last edited: