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

TFS [1.X] Lootwand

highsanta

Advanced OT User
Joined
Dec 20, 2023
Messages
529
Solutions
6
Reaction score
206
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)
 
for some reason I can not create threads in resource boards?
well anyways here is another one:
Lua:
function onThink(interval, lastExecution)
    local players = Game.getPlayers()
    for i = 1, #players do
        local player = players[i]
        if player:isPlayer() then
            local expToAdd = 1
            local storageValue = player:getStorageValue("55656")
            if storageValue > 1 then
                expToAdd = storageValue
            end
         
            local skillTriesToAdd = 1
         
            local skillsMultiplier = player:getStorageValue("55657")
            if skillsMultiplier > 1 then
                skillTriesToAdd = skillsMultiplier
            end
         
            local manaSpentToAdd = 1
         
            local manamultiplier = player:getStorageValue("55658")
            if manamultiplier > 1 then
                manaSpentToAdd = manamultiplier
            end
            --if player:getCondition(CONDITION_DRUNK) then
            --    expToAdd = expToAdd * 2
            --    skillTriesToAdd = skillTriesToAdd * 2
            --    manaSpentToAdd = manaSpentToAdd * 2
            --end
         
            if player:getStorageValue(156001) == 1 then
                player:addExperience(expToAdd)
            else
                player:addExperience(expToAdd, true)
            end
         
            player:addManaSpent(manaSpentToAdd)
            player:addSkillTries(SKILL_FIST, skillTriesToAdd)
            player:addSkillTries(SKILL_CLUB, skillTriesToAdd)
            player:addSkillTries(SKILL_SWORD, skillTriesToAdd)
            player:addSkillTries(SKILL_AXE, skillTriesToAdd)
            player:addSkillTries(SKILL_DISTANCE, skillTriesToAdd)
            player:addSkillTries(SKILL_SHIELD, skillTriesToAdd)
            local currentStorageValue = player:getStorageValue(1444444)
            player:setStorageValue(1444444, currentStorageValue + 1)
        end
    end
    return true
end

this is onThink interval 1000 global event that will add skills and experience via Idle system on the server you can use the example to make upgrades via storage
you can turn off display per player choice via storage 156001
Post automatically merged:

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)
why would I check if item is an item if its not in the table its not gonna be sellable everything else Does not matter. also if the gold goes onto ground that is even better because lazy botters will forget. anyways it is placeholder code until I add the appraisal skill to it hence 1/10 chance to activate i needed different debug. my code is never to go its adjust to your own personal needs and taste along with reference and Idea that it shares but i appreciate your effort next time dont bother making 10 quotes but just put code in 1 and add comments like chat gpt would If you put right prompts into it not having to even move your finger coding btw. if your logic is flawless so will the output of chatgpt be but I wont do it for pseudocode
 
Last edited:
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()

Thread should be "Sell Items onUse Item" , Lootwand what means ? The item used is a wand ?
Use GPT is good in order to learn some things, for create complete scripts you should know first how works the code in general.
 
Thread should be "Sell Items onUse Item" , Lootwand what means ? The item used is a wand ?
Use GPT is good in order to learn some things, for create complete scripts you should know first how works the code in general.
Lootwand you use it to sell an item. :) thats what I called this feature.
 
Back
Top