• 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!

TFS [1.X] Lootwand

highsanta

Advanced OT User
Joined
Dec 20, 2023
Messages
424
Solutions
3
Reaction score
180
Lua:
local lootTable = {
    { "axe", 2386, 7 },
    { "battle axe", 2378, 80 },
    { "battle hammer", 2417, 120 },
    { "battle shield", 2513, 95 },
    { "bone club", 2449, 5 },
    { "bone sword", 2450, 20 },
    { "brass armor", 2465, 150 },
    { "brass helmet", 2460, 30 },
    { "brass legs", 2478, 49 },
    { "brass shield", 2511, 25 },
    { "carlin sword", 2395, 118 },
    { "chain armor", 2464, 70 },
    { "chain helmet", 2458, 17 },
    { "chain legs", 2648, 25 },
    { "club", 2382, 1 },
    { "coat", 2651, 1 },
    { "copper shield", 2530, 50 },
    { "crowbar", 2416, 50 },
    { "dagger", 2379, 2 },
    { "double axe", 2387, 260 },
    { "doublet", 2485, 3 },
    { "dwarven shield", 2525, 100 },
    { "fire sword", 2392, 1000 },
    { "halberd", 2381, 400 },
    { "hand axe", 2380, 4 },
    { "hatchet", 2388, 25 },
    { "iron helmet", 2459, 150 },
    { "jacket", 2650, 1 },
    { "katana", 2412, 35 },
    { "leather armor", 2467, 12 },
    { "leather boots", 2643, 2 },
    { "leather helmet", 2461, 4 },
    { "leather legs", 2649, 9 },
    { "legion helmet", 2480, 22 },
    { "longsword", 2397, 51 },
    { "mace", 2398, 30 },
    { "morning star", 2394, 100 },
    { "orcish axe", 2428, 350 },
    { "plate armor", 2463, 400 },
    { "plate legs", 2647, 115 },
    { "plate shield", 2510, 45 },
    { "rapier", 2384, 5 },
    { "sabre", 2385, 12 },
    { "scale armor", 2483, 75 },
    { "short sword", 2406, 10 },
    { "sickle", 2405, 3 },
    { "small axe", 2559, 5 },
    { "soldier helmet", 2481, 16 },
    { "spike sword", 2383, 240 },
    { "steel helmet", 2457, 293 },
    { "steel shield", 2509, 80 },
    { "studded armor", 2484, 25 },
    { "studded club", 2448, 10 },
    { "studded helmet", 2482, 20 },
    { "studded legs", 2468, 15 },
    { "studded shield", 2526, 16 },
    { "sword", 2376, 25 },
    { "throwing knife", 2410, 2 },
    { "two handed sword", 2377, 450 },
    { "viking helmet", 2473, 66 },
    { "viking shield", 2531, 85 },
    { "war hammer", 2391, 470 },
    { "wooden shield", 2512, 5 }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    -- Get the item id of the target
    local targetItemId = target.itemid
    
    -- Check if the target item id is in the loot table
    local isInLootTable = false
    local itemPrice = 0
    local itemName = ""
    for _, lootItem in ipairs(lootTable) do
        if lootItem[2] == targetItemId then
            isInLootTable = true
            itemPrice = lootItem[3]
            itemName = lootItem[1]
            break
        end
    end
    
    -- If the target item is not found in the loot table, return false
    if not isInLootTable then
        print("Item with item id " .. targetItemId .. " is not in the loot table.")
        return false
    end
    
    -- Calculate the amount of money to give to the player
    local moneyToAdd = math.floor(itemPrice / 2)
    
    -- Check if the item sells for full price (1 in 10 chance)
    if math.random(10) == 1 then
        moneyToAdd = itemPrice
        player:say("[LWS] Sold " .. itemName .. " for " .. moneyToAdd .. " gold.", TALKTYPE_ORANGE_1)
        print("[DEBUG] [LWS] Sold " .. itemName .. " for " .. moneyToAdd .. " gold.")
    else
        player:say("You sold " .. itemName .. " for " .. moneyToAdd .. " gold.", TALKTYPE_ORANGE_1)
        print("[DEBUG] Sold " .. itemName .. " for " .. moneyToAdd .. " gold.")
    end
    
    -- Add money to the player
    player:addMoney(moneyToAdd)
    
    -- Remove the item from the player's inventory
    player:removeItem(targetItemId, 1)
    
    -- Debug print of the item used on what
    print("[DEBUG] Item with item id " .. targetItemId .. " was used on " .. itemName)
    
    return true -- Return true if the item usage is successful
end
Post automatically merged:

player:removeItem(targetItemId, 1)
chjange target:remove()
Post automatically merged:

player:removeItem(targetItemId, 1)
chjange target:remove()
 
Last edited:
Okay, like.. what is this? This is why you shouldn't use chatgpt.

You need to check that the target exists and is an item..

So change this..
Lua:
local targetItemId = target.itemid
to this..
Lua:
if not target or not target:isItem() then
     return true
end

local targetItemId = target:getId()

Your entire loop is unnecessary.

Instead of a table like this..
Lua:
local lootTable = {
    { "axe", 2386, 7 },
    { "battle axe", 2378, 80 },
change it for this..
Lua:
local lootTable = {
    [2386] = 7,
    [2378] = 80,

-- or this
local lootTable = {
    [2386] = {price = 7},
    [2378] = {price = 80},

Then you can omit the entire loop..

change this
Lua:
    -- Check if the target item id is in the loot table
    local isInLootTable = false
    local itemPrice = 0
    local itemName = ""
    for _, lootItem in ipairs(lootTable) do
        if lootItem[2] == targetItemId then
            isInLootTable = true
            itemPrice = lootItem[3]
            itemName = lootItem[1]
            break
        end
    end
 
    -- If the target item is not found in the loot table, return false
    if not isInLootTable then
        print("Item with item id " .. targetItemId .. " is not in the loot table.")
        return false
    end
to this
Lua:
local lootInfo = lootTable[targetItemId]

if not lootInfo then
    print("Item with item id " .. targetItemId .. " is not in the loot table.")
    return true
end

This section, you can get rid of the redundancy

so change this
Lua:
    -- Calculate the amount of money to give to the player
    local moneyToAdd = math.floor(itemPrice / 2)
 
    -- Check if the item sells for full price (1 in 10 chance)
    if math.random(10) == 1 then
        moneyToAdd = itemPrice
        player:say("[LWS] Sold " .. itemName .. " for " .. moneyToAdd .. " gold.", TALKTYPE_ORANGE_1)
        print("[DEBUG] [LWS] Sold " .. itemName .. " for " .. moneyToAdd .. " gold.")
    else
        player:say("You sold " .. itemName .. " for " .. moneyToAdd .. " gold.", TALKTYPE_ORANGE_1)
        print("[DEBUG] Sold " .. itemName .. " for " .. moneyToAdd .. " gold.")
    end

for this
Lua:
local moneyToAdd = lootInfo.price
 
-- Check if the item sells for full price (1 in 10 chance)
if math.random(10) ~= 1 then
    moneyToAdd = math.floor(moneyToAdd / 2)
end

local itemName = target:getName()
player:say("[LWS] Sold " .. itemName .. " for " .. moneyToAdd .. " gold.", TALKTYPE_ORANGE_1)
print("[DEBUG] [LWS] Sold " .. itemName .. " for " .. moneyToAdd .. " gold.")

and this is kind of minor.. but you should remove the item, then give the money. That way the money has the best chance of being successfully added to the player and not the ground.

so change this
Lua:
    -- Add money to the player
    player:addMoney(moneyToAdd)
 
    -- Remove the item from the player's inventory
    player:removeItem(targetItemId, 1)
for this

Lua:
target:remove(1)
player:addMoney(moneyToAdd)
 
Back
Top