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

Lua Tree cutting script - randomly stops working

Dercas

Active Member
Joined
Nov 15, 2008
Messages
86
Reaction score
28
Hello,

I've found here on the forum a script for tree cutting:

Lua:
local config = {
    trees = {2701,2702,2703,2704,2705,2706,2707,2708,2709,2710,2712,2717,2718,2720,2722},
    t = {
        [{1, 100}] = {tree = 2701},
        [{101, 200}] = {tree = 2702},
        [{201, 300}] = {tree = 2703},
        [{301, 400}] = {tree = 2704},
        [{401, 500}] = {tree = 2705},
        [{501, 600}] = {tree = 2706},
        [{601, 700}] = {tree = 2707},
        [{701, 800}] = {tree = 2708},
        [{801, 900}] = {tree = 2709},
        [{901, 1000}] = {tree = 2710},
        [{1001, 1100}] = {tree = 2712},
        [{1101, 1200}] = {tree = 2717},
        [{1201, 1300}] = {tree = 2718},
        [{1301, 1400}] = {tree = 2720},
        [{1401, 1500}] = {tree = 2722}
        },
    level = 15,
    skill = SKILL_AXE,
    skillReq = 10,
    effect = CONST_ME_BLOCKHIT,
    addTries = 100,
    branches = 6432,
    msgType = MESSAGE_EVENT_ADVANCE,
    soul = 3,
    minutes = 5
}

local t = {
    [{1, 500}] = {msg = "You choped the tree and got some wood", item = 5901, amountmax = 3},
    [{501, 750}] = {msg = "You have damaged your axe and it broke!", destroy = true},
    [{751, 1550}] = {msg = "You choped the tree down, but the the wood was not good."},
    [{1551, 1650}] = {msg = "oh no the tree had a Wasps nest in it!", summon = "Wasp"},
    [{1651, 1750}] = {msg = "You hurt your self while choping the tree down", damage = {1, 100}},
    [{1751, 2000}] = {msg = "You choped the tree and got some wood", item = 5901, amountmax = 5},
    [{2001, 2250}] = {msg = "A cat that was stuck jumped from the choped tree", summon = "Cat"},
    [{2251, 2500}] = {msg = "There was a bird's nest in the tree and you took some eggs from the nest", item = 2695, amountmax = 5},
    [{2501, 2750}] = {msg = "Hahaa! You found a sack with coins in it!", item = 2148, amountmax = 50},
    [{2751, 3000}] = {msg = "A rat jumped at you!", summon = "Rat"}
    }

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if isInArray(config.trees, itemEx.itemid) and config.level <= getPlayerLevel(cid) and config.skillReq <= getPlayerSkill(cid, config.skill) and config.soul <= getPlayerSoul(cid) then
        local v, amount, damage = math.random(3000), 1, nil
        for i, k in pairs(t) do
            if v >= i[1] and v <= i[2] then
                if k.destroy then
                    doRemoveItem(item.uid)
                end
                if k.summon then
                    doCreateMonster(k.summon, toPosition)
                end
                if k.damage then
                    damage = math.random(k.damage[1], k.damage[2])
                    doCreatureAddHealth(cid, - damage)
                    doSendMagicEffect(getThingPos(cid), CONST_ME_DRAWBLOOD)
                    doSendAnimatedText(getThingPos(cid), damage, TEXTCOLOR_RED)

                end
                if k.item then
                    if k.amountmax then
                        amount = math.random(k.amountmax)
                    end
                    doPlayerAddItem(cid, k.item, amount)
                end
                if k.msg then
                    local msg = k.msg
                    doPlayerSendTextMessage(cid, config.msgType, msg)
                end
                
                local function newTrees(parameter)
                    local tree = getThingfromPos{x = parameter.position.x, y = parameter.position.y, z = parameter.position.z, stackpos = 1}
                    for i2, k2 in pairs(config.t) do
                        local v2 = math.random(1500), 1, nil
                        if v2 >= i2[1] and v2 <= i2[2] then
                            if k2.tree then
                                if (tree.itemid == config.branches) then
                                    doTransformItem(tree.uid, k2.tree)
                                end
                            end
                        end       
                    end
                end   
                addEvent(newTrees, config.minutes*60*1000, {position = toPosition, cid = cid})   
                doTransformItem(itemEx.uid, config.branches)
                doPlayerAddSoul(cid, -config.soul)
                doSendMagicEffect(toPosition, k.destroy and CONST_ME_HITAREA or config.effect)   
                return doPlayerAddSkillTry(cid, config.skill, config.addTries)
            end
        end

  end   
    return doPlayerSendCancel(cid, "Either this tree can't be choped or you don't have enough experience, skill or soul to chop this tree.")
end

So the code should work:
1. Use axe on a tree
2. random thing is happening (you get wood/gold/spawn a rat etc)
3. tree transforms into branches
4. after XYZ time branches transform into a random tree

It works just fine at most of the times (I just had to replace doSendAnimatedText with doCreatureSay as I'm working on TFS 1.4.2 for 10.98 and doSendAnimatedText is no longer supported).

The problem with the script is that it stops randomly working. I mean, I can cut a tree with an axe, it will transform into branches, give a random effect, grow back. One, two times it works. On the third try it will be cut again, it will give a random effect but won't grow back - branches will remain on the ground. No matter how long I shall wait. The trees stop growing back randomly. Sometimes it's first cut, sometimes after 5 times.

Any idea how to fix that? There's no errors in the console.


Thank you in advance!

Cheers
 
Solution
Thank you:)
I'll have a look on compat.lua (tbh, didn't know about it earlier)

I've tried the script and here's what happened: it shouted an error regarding addSouls (but i removed S at the end & its fine)
& now the engine is shouting:

View attachment 82605


Despite this error (lack of the magic effect), the trees are being cut (they do transform into branches). I do get wood/gold/a monster is spawn. It takes soul from the character.
But the trees do not grow back at all

I could remove part with the magic effect (it's optional really) but trees growing back is the essential feature.

Your further help will be more than appreciated (there's no rush - even if you help me tomorrow, the day after tomorrow or in a week time I will be grateful)...
I haven't tested it yet because I'm at work. If there are any errors, I can correct them when I get home, or someone else can correct them for you.

But from what I noticed, your profile has been registered since 2008, and you still don't understand what already exists within the server data/lib/compat/compat.lua? Everything is explained there about how to convert old scripts to TFS 1.x. For example, a function that is 0.x or older is used like this: 'doPlayerAddSoul', and in TFS 1.x, it is used like this: 'player:addSoul'. Just take a look at your compat.lua file; it's simple.
lol

Lua:
local config = {
    trees = {2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2709, 2710, 2712, 2717, 2718, 2720, 2722},
    t = {
        [{1, 100}] = {tree = 2701},
        [{101, 200}] = {tree = 2702},
        [{201, 300}] = {tree = 2703},
        [{301, 400}] = {tree = 2704},
        [{401, 500}] = {tree = 2705},
        [{501, 600}] = {tree = 2706},
        [{601, 700}] = {tree = 2707},
        [{701, 800}] = {tree = 2708},
        [{801, 900}] = {tree = 2709},
        [{901, 1000}] = {tree = 2710},
        [{1001, 1100}] = {tree = 2712},
        [{1101, 1200}] = {tree = 2717},
        [{1201, 1300}] = {tree = 2718},
        [{1301, 1400}] = {tree = 2720},
        [{1401, 1500}] = {tree = 2722}
    },
    level = 15,
    skill = SKILL_AXE,
    skillReq = 10,
    effect = CONST_ME_BLOCKHIT,
    addTries = 100,
    branches = 6432,
    msgType = MESSAGE_EVENT_ADVANCE,
    soul = 3,
    minutes = 5
}

local t = {
    [{1, 500}] = {msg = "You chopped the tree and got some wood", item = 5901, amountmax = 3},
    [{501, 750}] = {msg = "You have damaged your axe and it broke!", destroy = true},
    [{751, 1550}] = {msg = "You chopped the tree down, but the wood was not good."},
    [{1551, 1650}] = {msg = "Oh no, the tree had a wasp nest in it!", summon = "Wasp"},
    [{1651, 1750}] = {msg = "You hurt yourself while chopping the tree down", damage = {1, 100}},
    [{1751, 2000}] = {msg = "You chopped the tree and got some wood", item = 5901, amountmax = 5},
    [{2001, 2250}] = {msg = "A cat that was stuck jumped from the chopped tree", summon = "Cat"},
    [{2251, 2500}] = {msg = "There was a bird's nest in the tree and you took some eggs from the nest", item = 2695, amountmax = 5},
    [{2501, 2750}] = {msg = "Hahaa! You found a sack with coins in it!", item = 2148, amountmax = 50},
    [{2751, 3000}] = {msg = "A rat jumped at you!", summon = "Rat"}
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    if not player then
        return false
    end
   
    if isInArray(config.trees, itemEx.itemid) and config.level <= player:getLevel() and config.skillReq <= player:getSkillLevel(config.skill) and config.soul <= player:getSoul() then
        local v = math.random(3000)
        for i, k in pairs(t) do
            if v >= i[1] and v <= i[2] then
                if k.destroy then
                    Item(itemEx.uid):remove(1)
                end
                if k.summon then
                    Game.createMonster(k.summon, toPosition)
                end
                if k.damage then
                    local damage = math.random(k.damage[1], k.damage[2])
                    player:addHealth(-damage)
                    player:sendMagicEffect(getThingPos(cid), CONST_ME_DRAWBLOOD)
                    Game.sendAnimatedText(getThingPos(cid), damage, TEXTCOLOR_RED)
                end
                if k.item then
                    local amount = k.amountmax and math.random(k.amountmax) or 1
                    player:addItem(k.item, amount)
                end
                if k.msg then
                    player:sendTextMessage(config.msgType, k.msg)
                end

                local function newTrees()
                    local tree = getThingfromPos(toPosition)
                    for i2, k2 in pairs(config.t) do
                        local v2 = math.random(1500)
                        if v2 >= i2[1] and v2 <= i2[2] then
                            if k2.tree and tree.itemid == config.branches then
                                Item(tree.uid):transform(k2.tree)
                            end
                        end
                    end
                end
                addEvent(newTrees, config.minutes * 60 * 1000)
                Item(itemEx.uid):transform(config.branches)
                player:addSouls(-config.soul)
                player:sendMagicEffect(toPosition, k.destroy and CONST_ME_HITAREA or config.effect)
                return player:addSkillTries(config.skill, config.addTries)
            end
        end
    end
    return player:sendCancelMessage("Either this tree can't be chopped or you don't have enough experience, skill, or soul to chop this tree.")
end
Post automatically merged:

Another detail: regarding the 'Game.sendAnimatedText' function, if it doesn't exist in your source, then just make the alteration here.
Lua:
if k.damage then
    local damage = math.random(k.damage[1], k.damage[2])
    player:addHealth(-damage)
    player:sendMagicEffect(getThingPos(cid), CONST_ME_DRAWBLOOD)
    player:say("OUCH! You received " .. damage .. " damage!", TALKTYPE_MONSTER_SAY)
end

or

Lua:
if k.damage then
    local damage = math.random(k.damage[1], k.damage[2])
    player:addHealth(-damage)
    player:sendMagicEffect(getThingPos(cid), CONST_ME_DRAWBLOOD)
    Creature(cid):say("OUCH! You received " .. damage .. " damage!", TALKTYPE_MONSTER_SAY)
end
 
I haven't tested it yet because I'm at work. If there are any errors, I can correct them when I get home, or someone else can correct them for you.

But from what I noticed, your profile has been registered since 2008, and you still don't understand what already exists within the server data/lib/compat/compat.lua? Everything is explained there about how to convert old scripts to TFS 1.x. For example, a function that is 0.x or older is used like this: 'doPlayerAddSoul', and in TFS 1.x, it is used like this: 'player:addSoul'. Just take a look at your compat.lua file; it's simple.
lol

Lua:
local config = {
    trees = {2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2709, 2710, 2712, 2717, 2718, 2720, 2722},
    t = {
        [{1, 100}] = {tree = 2701},
        [{101, 200}] = {tree = 2702},
        [{201, 300}] = {tree = 2703},
        [{301, 400}] = {tree = 2704},
        [{401, 500}] = {tree = 2705},
        [{501, 600}] = {tree = 2706},
        [{601, 700}] = {tree = 2707},
        [{701, 800}] = {tree = 2708},
        [{801, 900}] = {tree = 2709},
        [{901, 1000}] = {tree = 2710},
        [{1001, 1100}] = {tree = 2712},
        [{1101, 1200}] = {tree = 2717},
        [{1201, 1300}] = {tree = 2718},
        [{1301, 1400}] = {tree = 2720},
        [{1401, 1500}] = {tree = 2722}
    },
    level = 15,
    skill = SKILL_AXE,
    skillReq = 10,
    effect = CONST_ME_BLOCKHIT,
    addTries = 100,
    branches = 6432,
    msgType = MESSAGE_EVENT_ADVANCE,
    soul = 3,
    minutes = 5
}

local t = {
    [{1, 500}] = {msg = "You chopped the tree and got some wood", item = 5901, amountmax = 3},
    [{501, 750}] = {msg = "You have damaged your axe and it broke!", destroy = true},
    [{751, 1550}] = {msg = "You chopped the tree down, but the wood was not good."},
    [{1551, 1650}] = {msg = "Oh no, the tree had a wasp nest in it!", summon = "Wasp"},
    [{1651, 1750}] = {msg = "You hurt yourself while chopping the tree down", damage = {1, 100}},
    [{1751, 2000}] = {msg = "You chopped the tree and got some wood", item = 5901, amountmax = 5},
    [{2001, 2250}] = {msg = "A cat that was stuck jumped from the chopped tree", summon = "Cat"},
    [{2251, 2500}] = {msg = "There was a bird's nest in the tree and you took some eggs from the nest", item = 2695, amountmax = 5},
    [{2501, 2750}] = {msg = "Hahaa! You found a sack with coins in it!", item = 2148, amountmax = 50},
    [{2751, 3000}] = {msg = "A rat jumped at you!", summon = "Rat"}
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    if not player then
        return false
    end
  
    if isInArray(config.trees, itemEx.itemid) and config.level <= player:getLevel() and config.skillReq <= player:getSkillLevel(config.skill) and config.soul <= player:getSoul() then
        local v = math.random(3000)
        for i, k in pairs(t) do
            if v >= i[1] and v <= i[2] then
                if k.destroy then
                    Item(itemEx.uid):remove(1)
                end
                if k.summon then
                    Game.createMonster(k.summon, toPosition)
                end
                if k.damage then
                    local damage = math.random(k.damage[1], k.damage[2])
                    player:addHealth(-damage)
                    player:sendMagicEffect(getThingPos(cid), CONST_ME_DRAWBLOOD)
                    Game.sendAnimatedText(getThingPos(cid), damage, TEXTCOLOR_RED)
                end
                if k.item then
                    local amount = k.amountmax and math.random(k.amountmax) or 1
                    player:addItem(k.item, amount)
                end
                if k.msg then
                    player:sendTextMessage(config.msgType, k.msg)
                end

                local function newTrees()
                    local tree = getThingfromPos(toPosition)
                    for i2, k2 in pairs(config.t) do
                        local v2 = math.random(1500)
                        if v2 >= i2[1] and v2 <= i2[2] then
                            if k2.tree and tree.itemid == config.branches then
                                Item(tree.uid):transform(k2.tree)
                            end
                        end
                    end
                end
                addEvent(newTrees, config.minutes * 60 * 1000)
                Item(itemEx.uid):transform(config.branches)
                player:addSouls(-config.soul)
                player:sendMagicEffect(toPosition, k.destroy and CONST_ME_HITAREA or config.effect)
                return player:addSkillTries(config.skill, config.addTries)
            end
        end
    end
    return player:sendCancelMessage("Either this tree can't be chopped or you don't have enough experience, skill, or soul to chop this tree.")
end

First of all, thank you! I shall test it within a few minutes & edit the post to let you know what's the outcome.

Secondly, yes, I'm registered on otland since 2008 and I used to be very active member of OTS community in 2006-2009 on some (non existing now) forums. I had some 7.4/7.6 servers of my own. I remember the times when XML servers were more popular than sql ones & when 99% of servers were 'orshabaal & UE' servers. A lot changed since then. I'm coming back to OTS world after years and had no programming, scripting experience in the meantime... So, please, don't be hard on me.
 
I'm coming back to OTS world after years and had no programming, scripting experience in the meantime... So, please, don't be hard on me.
I didn't know that, but I understand, okay. It's worth studying about 'compat.lua'; in a few days or months, you'll be able to develop easily. Hope I helped! 😉
 
I didn't know that, but I understand, okay. It's worth studying about 'compat.lua'; in a few days or months, you'll be able to develop easily. Hope I helped! 😉
Thank you:)
I'll have a look on compat.lua (tbh, didn't know about it earlier)

I've tried the script and here's what happened: it shouted an error regarding addSouls (but i removed S at the end & its fine)
& now the engine is shouting:

1709301619628.png


Despite this error (lack of the magic effect), the trees are being cut (they do transform into branches). I do get wood/gold/a monster is spawn. It takes soul from the character.
But the trees do not grow back at all

I could remove part with the magic effect (it's optional really) but trees growing back is the essential feature.

Your further help will be more than appreciated (there's no rush - even if you help me tomorrow, the day after tomorrow or in a week time I will be grateful)

//edit
I've removed the magic effect part from the script & the trees still don't grow back just FYI
 
Thank you:)
I'll have a look on compat.lua (tbh, didn't know about it earlier)

I've tried the script and here's what happened: it shouted an error regarding addSouls (but i removed S at the end & its fine)
& now the engine is shouting:

View attachment 82605


Despite this error (lack of the magic effect), the trees are being cut (they do transform into branches). I do get wood/gold/a monster is spawn. It takes soul from the character.
But the trees do not grow back at all

I could remove part with the magic effect (it's optional really) but trees growing back is the essential feature.

Your further help will be more than appreciated (there's no rush - even if you help me tomorrow, the day after tomorrow or in a week time I will be grateful)

//edit
I've removed the magic effect part from the script & the trees still don't grow back just FYI
Not tested but should be good. It's best to not place any functions inside of another function scope., as it will be recreating that function everytime, when it only should be declared once.

Lua:
local config = {
    trees = {2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2709, 2710, 2712, 2717, 2718, 2720, 2722},
    t = {
        [{1, 100}] = {tree = 2701},
        [{101, 200}] = {tree = 2702},
        [{201, 300}] = {tree = 2703},
        [{301, 400}] = {tree = 2704},
        [{401, 500}] = {tree = 2705},
        [{501, 600}] = {tree = 2706},
        [{601, 700}] = {tree = 2707},
        [{701, 800}] = {tree = 2708},
        [{801, 900}] = {tree = 2709},
        [{901, 1000}] = {tree = 2710},
        [{1001, 1100}] = {tree = 2712},
        [{1101, 1200}] = {tree = 2717},
        [{1201, 1300}] = {tree = 2718},
        [{1301, 1400}] = {tree = 2720},
        [{1401, 1500}] = {tree = 2722}
    },
    actions = {
        [{1, 500}] = {msg = "You chopped the tree and got some wood", item = 5901, amountmax = 3},
        [{501, 750}] = {msg = "You have damaged your axe and it broke!", destroy = true},
        [{751, 1550}] = {msg = "You chopped the tree down, but the wood was not good."},
        [{1551, 1650}] = {msg = "Oh no, the tree had a wasp nest in it!", summon = "Wasp"},
        [{1651, 1750}] = {msg = "You hurt yourself while chopping the tree down", damage = {1, 100}},
        [{1751, 2000}] = {msg = "You chopped the tree and got some wood", item = 5901, amountmax = 5},
        [{2001, 2250}] = {msg = "A cat that was stuck jumped from the chopped tree", summon = "Cat"},
        [{2251, 2500}] = {msg = "There was a bird's nest in the tree and you took some eggs from the nest", item = 2695, amountmax = 5},
        [{2501, 2750}] = {msg = "Hahaa! You found a sack with coins in it!", item = 2148, amountmax = 50},
        [{2751, 3000}] = {msg = "A rat jumped at you!", summon = "Rat"}
    },
    level = 15,
    skill = SKILL_AXE,
    skillReq = 10,
    effect = CONST_ME_BLOCKHIT,
    addTries = 100,
    branches = 6432,
    msgType = MESSAGE_EVENT_ADVANCE,
    soul = 3,
    minutes = 5
}

local function newTree(pos)
    local tile = Tile(pos)
    if not tile then return end
   
    local tree = tile:getTopVisibleThing()
    if not tree or not tree:isItem() then return end
   
    local v2 = math.random(1500)
    for i2, k2 in pairs(config.t) do
        if v2 >= i2[1] and v2 <= i2[2] then
            if k2.tree and tree:getId() == config.branches then
                tree:transform(k2.tree)
            end
        end
    end
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    if not player then
        return false
    end
  
    if table.contains(config.trees, itemEx.itemid) and config.level <= player:getLevel() and config.skillReq <= player:getSkillLevel(config.skill) and config.soul <= player:getSoul() then
        local v = math.random(3000)
        for i, k in pairs(config.actions) do
            if v >= i[1] and v <= i[2] then
                if k.destroy then
                    item:remove(1)
                end
                if k.summon then
                    Game.createMonster(k.summon, toPosition, true)
                end
                if k.damage then
                    local damage = math.random(k.damage[1], k.damage[2])
                    player:addHealth(-damage)
                    player:getPosition():sendMagicEffect(CONST_ME_DRAWBLOOD)
                    Game.sendAnimatedText(player:getPosition(), damage, TEXTCOLOR_RED)
                end
                if k.item then
                    local amount = k.amountmax and math.random(k.amountmax) or 1
                    player:addItem(k.item, amount)
                end
                if k.msg then
                    player:sendTextMessage(config.msgType, k.msg)
                end

                addEvent(newTree, config.minutes * 60 * 1000, toPosition)
                itemEx:transform(config.branches)
                player:addSouls(-config.soul)
                toPosition:sendMagicEffect(k.destroy and CONST_ME_HITAREA or config.effect)
                player:addSkillTries(config.skill, config.addTries)
                return true
            end
        end
    end
    player:sendCancelMessage("Either this tree can't be chopped or you don't have enough experience, skill, or soul to chop this tree.")
    return true
end
 
Last edited:
Solution
Not tested but should be good. It's best to not place any functions inside of another function scope., as it will be recreating that function everytime, when it only should be declared once.

Lua:
local config = {
    trees = {2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2709, 2710, 2712, 2717, 2718, 2720, 2722},
    t = {
        [{1, 100}] = {tree = 2701},
        [{101, 200}] = {tree = 2702},
        [{201, 300}] = {tree = 2703},
        [{301, 400}] = {tree = 2704},
        [{401, 500}] = {tree = 2705},
        [{501, 600}] = {tree = 2706},
        [{601, 700}] = {tree = 2707},
        [{701, 800}] = {tree = 2708},
        [{801, 900}] = {tree = 2709},
        [{901, 1000}] = {tree = 2710},
        [{1001, 1100}] = {tree = 2712},
        [{1101, 1200}] = {tree = 2717},
        [{1201, 1300}] = {tree = 2718},
        [{1301, 1400}] = {tree = 2720},
        [{1401, 1500}] = {tree = 2722}
    },
    actions = {
        [{1, 500}] = {msg = "You chopped the tree and got some wood", item = 5901, amountmax = 3},
        [{501, 750}] = {msg = "You have damaged your axe and it broke!", destroy = true},
        [{751, 1550}] = {msg = "You chopped the tree down, but the wood was not good."},
        [{1551, 1650}] = {msg = "Oh no, the tree had a wasp nest in it!", summon = "Wasp"},
        [{1651, 1750}] = {msg = "You hurt yourself while chopping the tree down", damage = {1, 100}},
        [{1751, 2000}] = {msg = "You chopped the tree and got some wood", item = 5901, amountmax = 5},
        [{2001, 2250}] = {msg = "A cat that was stuck jumped from the chopped tree", summon = "Cat"},
        [{2251, 2500}] = {msg = "There was a bird's nest in the tree and you took some eggs from the nest", item = 2695, amountmax = 5},
        [{2501, 2750}] = {msg = "Hahaa! You found a sack with coins in it!", item = 2148, amountmax = 50},
        [{2751, 3000}] = {msg = "A rat jumped at you!", summon = "Rat"}
    },
    level = 15,
    skill = SKILL_AXE,
    skillReq = 10,
    effect = CONST_ME_BLOCKHIT,
    addTries = 100,
    branches = 6432,
    msgType = MESSAGE_EVENT_ADVANCE,
    soul = 3,
    minutes = 5
}

local function newTree(pos)
    local tile = Tile(pos)
    if not tile then return end
   
    local tree = tile:getTopVisibleThing()
    if not tree or not tree:isItem() then return end
   
    local v2 = math.random(1500)
    for i2, k2 in pairs(config.t) do
        if v2 >= i2[1] and v2 <= i2[2] then
            if k2.tree and tree:getId() == config.branches then
                tree:transform(k2.tree)
            end
        end
    end
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    if not player then
        return false
    end
  
    if table.contains(config.trees, itemEx.itemid) and config.level <= player:getLevel() and config.skillReq <= player:getSkillLevel(config.skill) and config.soul <= player:getSoul() then
        local v = math.random(3000)
        for i, k in pairs(config.actions) do
            if v >= i[1] and v <= i[2] then
                if k.destroy then
                    item:remove(1)
                end
                if k.summon then
                    Game.createMonster(k.summon, toPosition, false, true)
                end
                if k.damage then
                    local damage = math.random(k.damage[1], k.damage[2])
                    player:addHealth(-damage)
                    player:getPosition():sendMagicEffect(CONST_ME_DRAWBLOOD)
                    Game.sendAnimatedText(player:getPosition(), damage, TEXTCOLOR_RED)
                end
                if k.item then
                    local amount = k.amountmax and math.random(k.amountmax) or 1
                    player:addItem(k.item, amount)
                end
                if k.msg then
                    player:sendTextMessage(config.msgType, k.msg)
                end

                addEvent(newTree, config.minutes * 60 * 1000, toPosition)
                itemEx:transform(config.branches)
                player:addSouls(-config.soul)
                toPosition:sendMagicEffect(k.destroy and CONST_ME_HITAREA or config.effect)
                player:addSkillTries(config.skill, config.addTries)
                return true
            end
        end
    end
    player:sendCancelMessage("Either this tree can't be chopped or you don't have enough experience, skill, or soul to chop this tree.")
    return true
end

Thank you! :)
It works almost perfectly.

When summon appears, it spawns on the tree position and it blocks the transform from branches to tree:

1709304577018.png

Is it possible to spawn the monster next to the tree position?
 
yeah sorry, didnt think about that.
C++:
// Game.createMonster(monsterName, position[, extended = false[, force = false[, magicEffect = CONST_ME_TELEPORT]]])

As you can see here, the third param is extended, while the fourth is forced.
If you pass true to forced, it will force the spawn on the tile (as long as the tile exists)
You can just pass true for extended instead, which will spawn on a nearby neighbour if the designated tile is not appropriate.

Change:
Lua:
Game.createMonster(k.summon, toPosition, false, true)
to:
Lua:
Game.createMonster(k.summon, toPosition, true)
 
made an adaptation for the RevScripts, just add it to data/scripts... and look for this line 'ItemId, change the ID you want and test it... it's working perfectly, okay.

ItemId = 39191, ---- here item <<<<

Lua:
local config = {
    trees = {2701, 2702, 2703, 2704, 2705, 2706, 2707, 2708, 2709, 2710, 2712, 2717, 2718, 2720, 2722},
    t = {
        [{1, 100}] = {tree = 2701},
        [{101, 200}] = {tree = 2702},
        [{201, 300}] = {tree = 2703},
        [{301, 400}] = {tree = 2704},
        [{401, 500}] = {tree = 2705},
        [{501, 600}] = {tree = 2706},
        [{601, 700}] = {tree = 2707},
        [{701, 800}] = {tree = 2708},
        [{801, 900}] = {tree = 2709},
        [{901, 1000}] = {tree = 2710},
        [{1001, 1100}] = {tree = 2712},
        [{1101, 1200}] = {tree = 2717},
        [{1201, 1300}] = {tree = 2718},
        [{1301, 1400}] = {tree = 2720},
        [{1401, 1500}] = {tree = 2722}
    },
    
    ItemId = 39191, ---- here item 
    
    level = 15,
    skill = SKILL_AXE,
    skillReq = 10,
    effect = CONST_ME_BLOCKHIT,
    addTries = 100,
    branches = 6432,
    msgType = MESSAGE_EVENT_ADVANCE,
    soul = 3,
    minutes = 5
}

local t = {
    [{1, 500}] = {msg = "You chopped the tree and got some wood", item = 5901, amountmax = 3},
    [{501, 750}] = {msg = "You have damaged your axe and it broke!", destroy = true},
    [{751, 1550}] = {msg = "You chopped the tree down, but the wood was not good."},
    [{1551, 1650}] = {msg = "Oh no, the tree had a wasp nest in it!", summon = "Wasp"},
    [{1651, 1750}] = {msg = "You hurt yourself while chopping the tree down", damage = {1, 100}},
    [{1751, 2000}] = {msg = "You chopped the tree and got some wood", item = 5901, amountmax = 5},
    [{2001, 2250}] = {msg = "A cat that was stuck jumped from the chopped tree", summon = "Cat"},
    [{2251, 2500}] = {msg = "There was a bird's nest in the tree and you took some eggs from the nest", item = 2695, amountmax = 5},
    [{2501, 2750}] = {msg = "Hahaa! You found a sack with coins in it!", item = 2148, amountmax = 50},
    [{2751, 3000}] = {msg = "A rat jumped at you!", summon = "Rat"}
}

local function newTrees(toPosition)
    local tile = Tile(toPosition)
    if not tile then
        return
    end
    
    local tree = tile:getTopVisibleThing()
    if not tree or not tree:isItem() or tree.itemid ~= config.branches then
        return
    end
    
    for i2, k2 in pairs(config.t) do
        if v >= i2[1] and v <= i2[2] then
            if k2.tree then
                tree:transform(k2.tree)
            end
        end
    end
end

local Action = Action()

function Action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not player then
        return false
    end

    local itemEx = Tile(toPosition):getItemById(target.itemid)
    if not itemEx then
        return false
    end

    if not isInArray(config.trees, itemEx.itemid) then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "This tree can't be chopped.")
        return false
    end

    if config.level > player:getLevel() then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your level is too low to chop this tree. Required level: " .. config.level)
        return false
    end

    local axeSkillLevel = player:getSkillLevel(config.skill)
    if config.skillReq > axeSkillLevel then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your axe skill level is too low to chop this tree. Required skill level: " .. config.skillReq .. ", Your skill level: " .. axeSkillLevel)
        return false
    end

    if config.soul > player:getSoul() then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You do not have enough soul to chop this tree.")
        return false
    end

    local v = math.random(3000)
    for i, k in pairs(t) do
        if v >= i[1] and v <= i[2] then
            if k.destroy then
                itemEx:remove(1)
            end
            if k.summon then
                Game.createMonster(k.summon, toPosition)
            end
            if k.damage then
                local damage = math.random(k.damage[1], k.damage[2])
                player:addHealth(-damage)
                player:say("OUCH! You received " .. damage .. " damage!", TALKTYPE_MONSTER_SAY)
            end
            if k.item then
                local amount = k.amountmax and math.random(k.amountmax) or 1
                player:addItem(k.item, amount)
            end
            if k.msg then
                player:sendTextMessage(config.msgType, k.msg)
            end

            newTrees(toPosition)
            addEvent(newTrees, config.minutes * 60 * 1000, toPosition)
            itemEx:transform(config.branches)
            player:getPosition():sendMagicEffect(k.destroy and CONST_ME_HITAREA or config.effect)
            player:addSkillTries(config.skill, config.addTries)
            player:addSoul(-config.soul)
            return true
        end
    end
    
  player:sendTextMessage(MESSAGE_EVENT_ADVANCE,"Debug: All conditions met.") 
    return true
end

Action:id(config.ItemId)
Action:register()
Post automatically merged:

yeah sorry, didnt think about that.
C++:
// Game.createMonster(monsterName, position[, extended = false[, force = false[, magicEffect = CONST_ME_TELEPORT]]])

As you can see here, the third param is extended, while the fourth is forced.
If you pass true to forced, it will force the spawn on the tile (as long as the tile exists)
You can just pass true for extended instead, which will spawn on a nearby neighbour if the designated tile is not appropriate.

Change:
Lua:
Game.createMonster(k.summon, toPosition, false, true)
to:
Lua:
Game.createMonster(k.summon, toPosition, true)
Oh, oops, didn't even notice that you had already corrected it! I was editing the script and posted it without realizing you had already done it, LOL.
 
Last edited:
You have to mark how it was resolved, whether it was my code or Fjorda's, because there are people searching for this script to know what the solution was, you know. I hope I understood correctly!
Sorry for the late reply. On the weekends I don't really check the forum/work on OTS

I've checked your version of the script (revscript) and it shouts the following error:
1709543768710.png

So, bascially, it cuts the tree, gives wood/gold/spawns a monster but the tree doesn't grow back


Fjorda script works fine
 
Back
Top