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

Solved TFS [1.2] Enchanting weapons

Shadow Dan

Sh4dowDan
Joined
Jun 5, 2010
Messages
344
Reaction score
88
Location
Poland
Script is working without any errors but gives only magic effect "CONST_ME_MAGIC_RED" it passed full script but weapon is not enchanted, also gems cannot be enchanted (mini message ingame - error sorry not possible when using on altar).
All id of items are ok, actions.xml is ok. I've checked it on lower version of my copy same TFS like 10.76 (now is 10.90) and it was working. That means some functions need to be changed.

I guess it's reason but didn't found yet how to fix it.
It just fail at transforming item when trying to match table.
Code:
local targetId = table.find(enchantedGems, item.itemid)
Code:
local subtype = target.type

Code:
local config = {
    manaCost = 300,
    soulCost = 2,
}

local spheres = {
    [7759] = {3, 7},
    [7760] = {1, 5},
    [7761] = {2, 6},
    [7762] = {4, 8}
}

local enchantableGems = {2147, 2146, 2149, 2150}
local enchantableItems = {2383, 7383, 7384, 7406, 7402, 2429, 2430, 7389, 7380, 2454, 2423, 2445, 7415, 7392, 2391, 2544, 8905}

local enchantingAltars = {
    {7504, 7505, 7506, 7507},
    {7508, 7509, 7510, 7511},
    {7516, 7517, 7518, 7519},
    {7512, 7513, 7514, 7515}
}

local enchantedGems = {7760, 7759, 7761, 7762}
local enchantedItems = {
    [2383] = {7744, 7763, 7854, 7869},
    [7383] = {7745, 7764, 7855, 7870},
    [7384] = {7746, 7765, 7856, 7871},
    [7406] = {7747, 7766, 7857, 7872},
    [7402] = {7748, 7767, 7858, 7873},
    [2429] = {7749, 7768, 7859, 7874},
    [2430] = {7750, 7769, 7860, 7875},
    [7389] = {7751, 7770, 7861, 7876},
    [7380] = {7752, 7771, 7862, 7877},
    [2454] = {7753, 7772, 7863, 7878},
    [2423] = {7754, 7773, 7864, 7879},
    [2445] = {7755, 7774, 7865, 7880},
    [7415] = {7756, 7775, 7866, 7881},
    [7392] = {7757, 7776, 7867, 7882},
    [2391] = {7758, 7777, 7868, 7883},
    [2544] = {7840, 7839, 7850, 7838},
    [8905] = {8906, 8907, 8909, 8908}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if isInArray({33268, 33269}, toPosition.x) and toPosition.y == 31830 and toPosition.z == 10 and player:getStorageValue(Storage.ElementalSphere.QuestLine) > 0 then
        if not isInArray(spheres[item.itemid], player:getVocation():getId()) then
            return true
        elseif isInArray({7915, 7916}, target.itemid) then
            player:say('Turn off the machine first.', TALKTYPE_MONSTER_SAY)
            return true
        else
            player:setStorageValue(Storage.ElementalSphere.MachineGemCount, math.max(1, player:getStorageValue(Storage.ElementalSphere.MachineGemCount) + 1))
            toPosition:sendMagicEffect(CONST_ME_PURPLEENERGY)
            item:transform(item.itemid, item.type - 1)
            return true
        end
    end

    if item.itemid == 2147 and target.itemid == 2342 then
        target:transform(2343)
        target:decay()
        item:remove(1)
        target:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
        return true
    end

    if item.itemid == 7760 and isInArray({9934, 10022}, target.itemid) then
        target:transform(9933)
        item:remove(1)
        toPosition:sendMagicEffect(CONST_ME_MAGIC_RED)
        return true
    end

    if isInArray(enchantableGems, item.itemid) then
        local subtype = item.type
        if subtype == 0 then
            subtype = 1
        end

        local mana = config.manaCost * subtype
        if player:getMana() < mana then
            player:sendCancelMessage(RETURNVALUE_NOTENOUGHMANA)
            return true
        end

        local soul = config.soulCost * subtype
        if player:getSoul() < soul then
            player:sendCancelMessage(RETURNVALUE_NOTENOUGHSOUL)
            return true
        end

        local targetId = table.find(enchantableGems, item.itemid)
        if not targetId or not isInArray(enchantingAltars[targetId], target.itemid) then
            return true
        end

        player:addMana(-mana)
        player:addSoul(-soul)
        item:transform(enchantedGems[targetId])
        player:addManaSpent(mana * configManager.getNumber(configKeys.RATE_MAGIC))
        player:getPosition():sendMagicEffect(CONST_ME_HOLYDAMAGE)
        return true
    end

    if item.itemid == 7761 and isInArray({9949, 9954}, target.itemid) then
        target:transform(target.itemid - 1)
        target:decay()
        item:remove(1)
        toPosition:sendMagicEffect(CONST_ME_MAGIC_GREEN)
        return true
    end

    if isInArray(enchantedGems, item.itemid) then
        if not isInArray(enchantableItems, target.itemid) then
            fromPosition:sendMagicEffect(CONST_ME_POFF)
            return true
        end

        local targetId = table.find(enchantedGems, item.itemid)
        if not targetId then
            return true
        end

        local subtype = target.type
        if not isInArray({2544, 8905}, target.itemid) then
            subtype = 1000
        end

        target:transform(enchantedItems[target.itemid][targetId], subtype)
        target:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
        item:remove(1)
        return true
    end
    return true
end

#Solved
Put table.find in your global.lua
Code:
function table.find(tab, value)
for i = 1, #tab do
if tab[i] == value then
return i
end
end
end
 
Last edited:
ok missunderstood you. Because thats not an error xD

Anyway, tahts pretty simple fix there. Use prints() and you see it yourself.
 
1. Reload actions
2. Using enchanted ruby on knight axe
3. Using small sapphire on altar
Code:
config_1
config_2
config_3
config_4
config_5
config_6
config_7
config_8
config_1
config_2
config_3
config_4
config_5
config_6
config_7
config_8
config_1
config_2
config_3
config_4
config_5
config_6
config_7
config_8
config_1
config_2
config_3
config_4
config_5
config_6
config_7
config_8
config_1
config_2
config_3
config_4
config_5
config_6
config_7
config_8
config_1
config_2
config_3
config_4
config_5
config_6
config_7
config_8
Code:
1
6
8
10
19
21
22
24
26
27
28
Fail at:
Code:
target:transform(enchantedItems[target.itemid][targetId], subtype)
Code:
1
6
8
10
11
13
15
17
Fail at:
Code:
if not targetId or not isInArray(enchantingAltars[targetId], target.itemid) then
Code:
print("config_1")
local config = {
    manaCost = 300,
    soulCost = 2,
}
print("config_2")
local spheres = {
    [7759] = {3, 7},
    [7760] = {1, 5},
    [7761] = {2, 6},
    [7762] = {4, 8}
}
print("config_3")
local enchantableGems = {2147, 2146, 2149, 2150}
print("config_4")
local enchantableItems = {2383, 7383, 7384, 7406, 7402, 2429, 2430, 7389, 7380, 2454, 2423, 2445, 7415, 7392, 2391, 2544, 8905}
print("config_5")
local enchantingAltars = {
    {7504, 7505, 7506, 7507},
    {7508, 7509, 7510, 7511},
    {7516, 7517, 7518, 7519},
    {7512, 7513, 7514, 7515}
}
print("config_6")
local enchantedGems = {7760, 7759, 7761, 7762}
print("config_7")
local enchantedItems = {
    [2383] = {7744, 7763, 7854, 7869},
    [7383] = {7745, 7764, 7855, 7870},
    [7384] = {7746, 7765, 7856, 7871},
    [7406] = {7747, 7766, 7857, 7872},
    [7402] = {7748, 7767, 7858, 7873},
    [2429] = {7749, 7768, 7859, 7874},
    [2430] = {7750, 7769, 7860, 7875},
    [7389] = {7751, 7770, 7861, 7876},
    [7380] = {7752, 7771, 7862, 7877},
    [2454] = {7753, 7772, 7863, 7878},
    [2423] = {7754, 7773, 7864, 7879},
    [2445] = {7755, 7774, 7865, 7880},
    [7415] = {7756, 7775, 7866, 7881},
    [7392] = {7757, 7776, 7867, 7882},
    [2391] = {7758, 7777, 7868, 7883},
    [2544] = {7840, 7839, 7850, 7838},
    [8905] = {8906, 8907, 8909, 8908}
}
print("config_8")
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
print("1")
    if isInArray({33268, 33269}, toPosition.x) and toPosition.y == 31830 and toPosition.z == 10 and player:getStorageValue(Storage.ElementalSphere.QuestLine) > 0 then
print("2")
        if not isInArray(spheres[item.itemid], player:getVocation():getId()) then
print("3")
            return true
        elseif isInArray({7915, 7916}, target.itemid) then
print("4")
            player:say('Turn off the machine first.', TALKTYPE_MONSTER_SAY)
            return true
        else
print("5")
            player:setStorageValue(Storage.ElementalSphere.MachineGemCount, math.max(1, player:getStorageValue(Storage.ElementalSphere.MachineGemCount) + 1))
            toPosition:sendMagicEffect(CONST_ME_PURPLEENERGY)
            item:transform(item.itemid, item.type - 1)
            return true
        end
    end
print("6")
    if item.itemid == 2147 and target.itemid == 2342 then
print("7")
        target:transform(2343)
        target:decay()
        item:remove(1)
        target:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
        return true
    end
print("8")
    if item.itemid == 7760 and isInArray({9934, 10022}, target.itemid) then
print("9")
        target:transform(9933)
        item:remove(1)
        toPosition:sendMagicEffect(CONST_ME_MAGIC_RED)
        return true
    end
print("10")
    if isInArray(enchantableGems, item.itemid) then
print("11")
        local subtype = item.type
        if subtype == 0 then
print("12")
            subtype = 1
        end
print("13")
        local mana = config.manaCost * subtype
        if player:getMana() < mana then
print("14")
            player:sendCancelMessage(RETURNVALUE_NOTENOUGHMANA)
            return true
        end
print("15")
        local soul = config.soulCost * subtype
        if player:getSoul() < soul then
print("16")
            player:sendCancelMessage(RETURNVALUE_NOTENOUGHSOUL)
            return true
        end

        local targetId = table.find(enchantableGems, item.itemid)
        if not targetId or not isInArray(enchantingAltars[targetId], target.itemid) then
print("17")
            return true
        end
print("18")
        player:addMana(-mana)
        player:addSoul(-soul)
        item:transform(enchantedGems[targetId])
        player:addManaSpent(mana * configManager.getNumber(configKeys.RATE_MAGIC))
        player:getPosition():sendMagicEffect(CONST_ME_HOLYDAMAGE)
        return true
    end
print("19")
    if item.itemid == 7761 and isInArray({9949, 9954}, target.itemid) then
print("20")
        target:transform(target.itemid - 1)
        target:decay()
        item:remove(1)
        toPosition:sendMagicEffect(CONST_ME_MAGIC_GREEN)
        return true
    end
print("21")
    if isInArray(enchantedGems, item.itemid) then
print("22")
        if not isInArray(enchantableItems, target.itemid) then
print("23")
            fromPosition:sendMagicEffect(CONST_ME_POFF)
            return true
        end
print("24")
        local targetId = table.find(enchantedGems, item.itemid)
        if not targetId then
print("25")
            return true
        end
print("26")
        local subtype = target.type
        if not isInArray({2544, 8905}, target.itemid) then
print("27")
            subtype = 1000
        end
print("28")
        target:transform(enchantedItems[target.itemid][targetId], subtype)
        target:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
        item:remove(1)
        return true
    end
print("29")
    return true
end

Looks like this is reason of all troubles.
Code:
local targetId = table.find(enchantableGems, item.itemid)
Any ideas how to fix? Where is function table.find?
 
Last edited:
not sure how the table.find works, its lua function you can google it.
but isInArray(enchantableGems, item.itemid)

will do the trick for you.
 
Is that answer? google it -.-? Trust me i've been trying multiple times.

Code:
function table.set(t) -- set of list
    local u ={}
        for _, v in ipairs(t) do 
            u[v]=true
        end
    return u
end

function table.find(f, l) -- find element v of l satisfyingf(v)
    for _, v in ipairs(l) do
        if f(v) then
            return v
        end
    end
    return nil
end
 
Last edited:
Was this issue ever resolved? I have this same problem.
Put each local variable or table after they have been defined in a print statement like this and then take a screen shot of the results.
Code:
local someVariable = someValue
print("name of the variable", type(someVariable) ~= 'table' someVariable or unpack(someVariable))
This is just a simple way to trouble shoot a script which doesn't throw an error.
 
Can I laugh?
be more specific in your requests.

Code:
local enchantableGems = {2147, 2146, 2149, 2150}
print("enchantableGems", type(enchantableGems) ~= 'table' enchantableGems or unpack(enchantableGems))

Code:
[Warning - Event::checkScript] Can not load script: scripts/other/enchanting.lua
data/actions/scripts/other/enchanting.lua:9: ')' expected near 'enchantableGems'
[Warning - Event::checkScript] Can not load script: scripts/other/enchanting.lua
data/actions/scripts/other/enchanting.lua:9: ')' expected near 'enchantableGems'
[Warning - Event::checkScript] Can not load script: scripts/other/enchanting.lua
data/actions/scripts/other/enchanting.lua:9: ')' expected near 'enchantableGems'
 
Last edited:
be more specific in your requests.

Code:
local enchantableGems = {2147, 2146, 2149, 2150}
print("enchantableGems", type(enchantableGems) ~= 'table' enchantableGems or unpack(enchantableGems))

Code:
[Warning - Event::checkScript] Can not load script: scripts/other/enchanting.lua
data/actions/scripts/other/enchanting.lua:9: ')' expected near 'enchantableGems'
[Warning - Event::checkScript] Can not load script: scripts/other/enchanting.lua
data/actions/scripts/other/enchanting.lua:9: ')' expected near 'enchantableGems'
[Warning - Event::checkScript] Can not load script: scripts/other/enchanting.lua
data/actions/scripts/other/enchanting.lua:9: ')' expected near 'enchantableGems'
I can not believe I need to hold your hand on this... why would you print a table that is defined outside of an interface?
Like this
Code:
    if isInArray(enchantableGems, item.itemid) then
        local subtype = item.type
        print('inside if subtype ', type(subtype) ~= 'table' and subtype or unpack(subtype))
        if subtype == 0 then
            subtype = 1
        end

        local mana = config.manaCost * subtype
        print('inside if mana ', type(mana) ~= 'table' and mana or unpack(mana))
        if player:getMana() < mana then
            player:sendCancelMessage(RETURNVALUE_NOTENOUGHMANA)
            return true
        end

        local soul = config.soulCost * subtype
        if player:getSoul() < soul then
            player:sendCancelMessage(RETURNVALUE_NOTENOUGHSOUL)
            return true
        end

        local targetId = table.find(enchantableGems, item.itemid)
        print('inside if targetId ', type(targetId) ~= 'table' and targetId or unpack(targetId))
        if not targetId or not isInArray(enchantingAltars[targetId], target.itemid) then
            return true
        end

        player:addMana(-mana)
        player:addSoul(-soul)
        item:transform(enchantedGems[targetId])
        player:addManaSpent(mana * configManager.getNumber(configKeys.RATE_MAGIC))
        player:getPosition():sendMagicEffect(CONST_ME_HOLYDAMAGE)
        return true
    end
 
The point of using print is to determine what values are being produced in the script, so that you can track down a problem especially when no error is thrown.
 
I can not believe I need to hold your hand on this... why would you print a table that is defined outside of an interface?

If I knew that, I wouldn't be asking.. however.. we are still here.

Code:
inside if mana  6000-- mana cost per 20 gems (300 per)
inside if subtype       20 -- # of gems in bag

this did not print on use of a non enchanted gem on an alter.
local targetId = table.find(enchantableGems, item.itemid)
print('inside if targetId ', type(targetId) ~= 'table' and targetId or unpack(targetId))
if not targetId or not isInArray(enchantingAltars[targetId], target.itemid) then
return true
end
 
If I knew that, I wouldn't be asking.. however.. we are still here.

Code:
inside if mana  6000-- mana cost per 20 gems (300 per)
inside if subtype       20 -- # of gems in bag

this did not print on use of a non enchanted gem on an alter.
The values I sent to print were just an example on how it can be used to trouble shoot a script, I am sorry I am not going to fix your script, you need to learn how the script itself works, print is just a tool.. you can't keep putting off the idea of not learning lua.

Just as you are frustrated and can't understand why someone won't just simply resolve the issue, I find it similarly odd that you won't take the time to learn the core language.

How do you expect to be anything in life if you won't even take the time to learn about even the basics of a language which is needed to help you build and understand something you are interested in?

I must be in the wrong community.. I thought this was a developers forum.. my mistake moving along.
 
You gave him a hammer and told to build a house. He just need a little explain how table.find works and example of using it.
It's support board, what's next? New rule don't post anything do it yourself? ^^
I will just make script on target without table.
 
There's probably a better way to do this, but I was in a hurry when I fixed it in my server

Code:
        local arrayElem = 0
       
        if item.itemid == 7759 then
            arrayElem = 2
        elseif item.itemid == 7760 then
            arrayElem = 1
        elseif item.itemid == 7761 then
            arrayElem = 3
        elseif item.itemid == 7762 then
            arrayElem = 4
        end
       
        target:transform(enchantedItems[target.itemid][arrayElem])
        target:decay()
        item:remove(1)
        target:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
        return true
 
You gave him a hammer and told to build a house. He just need a little explain how table.find works and example of using it.
It's support board, what's next? New rule don't post anything do it yourself? ^^
I will just make script on target without table.
There is plenty of things I have learned from people on these forums that I did not know how to do myself but I knew the languages involved.

90% of the people who post in support have thousands of posts in regards to their server of how to do something, at some point people need to learn these languages, will learning the languages make them super developers?

Initially no, but they will be headed in the right direction, every program/script starts small, hell when i started learning to code all i could do was print numbers to the browser window or dos prompt.

At least with lua accompanied with the tfs framework you can test or build cool things.

I am not giving anyone a hammer and telling them to build a house, rather these people want to build a house but refuse to use the tools or obtain an education which is needed and is freely available to them.

If me wanting others to strive to be do better for themselves makes me an asshole, then I am the biggest asshole you will ever encounter. :)
 
Is that answer? google it -.-? Trust me i've been trying multiple times.

Code:
function table.set(t) -- set of list
    local u ={}
        for _, v in ipairs(t) do
            u[v]=true
        end
    return u
end

function table.find(f, l) -- find element v of l satisfyingf(v)
    for _, v in ipairs(l) do
        if f(v) then
            return v
        end
    end
    return nil
end

If I knew that, I wouldn't be asking.. however.. we are still here.

Code:
inside if mana  6000-- mana cost per 20 gems (300 per)
inside if subtype       20 -- # of gems in bag

this did not print on use of a non enchanted gem on an alter.

Change the function table.find to this (or add it if you don't have):

Code:
function table.find(tab, value)
    for i = 1, #tab do
        if tab[i] == value then
            return i
        end
    end
end
 
Back
Top Bottom