n3crozzy
Member
- Joined
- Apr 23, 2021
- Messages
- 48
- Reaction score
- 14
Hi, I have a problem with the script. The script seems to work but not completely. It doesn't change my gp from bodies and other backpacks. It only changes from the main backpack and from the ground. Could someone explain to me what I'm doing wrong?
Maybe the script I have is written incorrectly?
Maybe the script I have is written incorrectly?
LUA:
function onUse(cid, item, fromPosition, itemEx, toPosition)
local player = Player(cid)
if not player then
return false
end
local goldToPlatinum = 100 -- 100 Gold Coins = 1 Platinum Coin
local platinumToCrystal = 100 -- 100 Platinum Coins = 1 Crystal Coin
local platinumToGold = 1 -- 1 Platinum Coin = 100 Gold Coins
local crystalToPlatinum = 1 -- 1 Crystal Coin = 100 Platinum Coins
local container = player:getSlotItem(CONST_SLOT_BACKPACK) -- Takes the player's backpack
if not container then
player:sendTextMessage(MESSAGE_INFO_DESCR, "You need a backpack to exchange coins.")
return true
end
-- Function to exchange coins in the backpack
local function exchangeCoinsInContainer(container, itemId, removeCount, addItemId, addCount, successMessage)
if not container then return false end
for i = 0, container:getSize() - 1 do
local item = container:getItem(i)
if item and item:getId() == itemId and item:getCount() >= removeCount then
item:remove(removeCount)
container:addItem(addItemId, addCount)
player:sendTextMessage(MESSAGE_INFO_DESCR, successMessage)
return true
end
end
return false
end
-- Function to exchange coins on the ground
local function exchangeCoinsOnGround(position, itemId, removeCount, addItemId, addCount, successMessage)
local tile = Tile(position)
if tile then
local item = tile:getItemById(itemId)
if item and item:getCount() >= removeCount then
item:remove(removeCount)
player:addItem(addItemId, addCount)
player:sendTextMessage(MESSAGE_INFO_DESCR, successMessage)
return true
end
end
return false
end
-- First we try to convert the coins from the ground
if item.itemid == 2148 then -- Gold Coin
if exchangeCoinsOnGround(fromPosition, 2148, 100, 2152, 1, "You have exchanged 100 Gold Coins for 1 Platinum Coin.") then
return true
end
elseif item.itemid == 2152 then -- Platinum Coin
if exchangeCoinsOnGround(fromPosition, 2152, 100, 2160, 1, "You have exchanged 100 Platinum Coins for 1 Crystal Coin.") then
return true
elseif exchangeCoinsOnGround(fromPosition, 2152, 1, 2148, 100, "You have exchanged 1 Platinum Coin for 100 Gold Coins.") then
return true
end
elseif item.itemid == 2160 then -- Crystal Coin
if exchangeCoinsOnGround(fromPosition, 2160, 1, 2152, 100, "You have exchanged 1 Crystal Coin for 100 Platinum Coins.") then
return true
end
end
-- If the exchange from the ground was not possible, we check the backpack
if item.itemid == 2148 then -- Gold Coin
if exchangeCoinsInContainer(container, 2148, 100, 2152, 1, "You have exchanged 100 Gold Coins for 1 Platinum Coin.") then
return true
end
elseif item.itemid == 2152 then -- Platinum Coin
if exchangeCoinsInContainer(container, 2152, 100, 2160, 1, "You have exchanged 100 Platinum Coins for 1 Crystal Coin.") then
return true
elseif exchangeCoinsInContainer(container, 2152, 1, 2148, 100, "You have exchanged 1 Platinum Coin for 100 Gold Coins.") then
return true
end
elseif item.itemid == 2160 then -- Crystal Coin
if exchangeCoinsInContainer(container, 2160, 1, 2152, 100, "You have exchanged 1 Crystal Coin for 100 Platinum Coins.") then
return true
end
end
return true
end
Last edited by a moderator: