• 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 TFS 1.2 Looking for advanced shovel script

SixNine

Active Member
Joined
Dec 12, 2018
Messages
452
Reaction score
41
Hello, looking for a advanced shovel script maybe anyone has a similar one, basically how it should work
  • There would be multiple types of shovels from wooden, to metal and lets say golden shovel. Each shovel differs in the number of uses and digs time which is lets say. Wooden Shovel 5s digging time, 5uses, Metal Shovel 3s digging time, 20uses, Golden Shovel 0.5s digging time, has one week unlimited usage and 25% higher find chance
  • You could dig objects that has lets say item id 19512 and actionid 9000, so basically you would click Use With... on shovel and hover it on that object and it would remove that object because you already digged, and once you dig it there would be a chance to find items X or find nothing.
  • After all uses are used the shovel would break
 
Solution
I believe I managed to finish the script as suggested by Fiorda. I used 'custom attributes' to store the 'uses' directly on the item, indicating when it was used and the remaining amount. I also added a visibility feature and an effect. Note: I don't have sprites for the PA, so I added sprites similar to the PICK as an example. If you don't have PA sprites, you can look for Pokémon ones that are free, just grab them there and add them to your sprite. Alternatively, you can use 'CONST_ME_HITAREA' for a visual effect when striking... Watch the video to see how it works xD.




Lua:
local shovels = {
    [2556] = { -- Wooden Shovel
        dig_time = 5,  -- Digging time in seconds
        uses = 5,  -- Number of uses...
Tested and it works, but it's quite basic.

Lua:
local shovels = {
    [2556] = { -- Wooden Shovel
        dig_time = 5,  -- Digging time in seconds
        uses = 5,  -- Number of uses
        find_chance = 0.1,  -- Chance to find an item (10%)
        possible_items = {{itemid = 2400, count = 3}, {itemid = 2401, count = 2}, {itemid = 2402, count = 1}}  -- Possible items with count
    },
    [2432] = { -- Metal Shovel
        dig_time = 3,
        uses = 20,
        find_chance = 0.15,
        possible_items = {{itemid = 2403, count = 1}, {itemid = 2404, count = 1}, {itemid = 2405, count = 2}}
    },
    [38864] = { -- Golden Shovel
        dig_time = 0.5,
        uses = math.huge,  -- Unlimited usage for one week
        find_chance = 0.25,
        expiration = os.time() + (7 * 24 * 60 * 60),  -- Expires in one week
        possible_items = {{itemid = 2406, count = 2}, {itemid = 2407, count = 1}, {itemid = 2408, count = 3}}
    }
}

local diggingInProgress = {}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local shovel = shovels[item.itemid]
    if not shovel then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You need to use a shovel to dig here.")
        return false
    end

    if target.actionid ~= 9000 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot dig here.")
        return false
    end

    -- Check if the golden shovel has expired
    if item.itemid == 38864 and shovel.expiration and os.time() > shovel.expiration then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Your golden shovel has expired.")
        return false
    end

    if diggingInProgress[player:getGuid()] then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are already digging. Please wait until it's finished.")
        return false
    end

    if shovel.uses <= 0 and not shovel.isBroken then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your shovel is broken.")
        shovel.isBroken = true
        diggingInProgress[player:getGuid()] = nil
        
        -- Create a hole in the ground if the shovel is broken
        local tile = Tile(target:getPosition())
        if tile then
            local ground = tile:getGround()
            if ground then
                local groundId = ground:getId()
                if groundId == 9043 then
                    Game.createItem(103, 1, target:getPosition()) -- Create a hole (ID 103) in the ground
                elseif groundId == 9044 then
                    Game.createItem(104, 1, target:getPosition()) -- Create a hole (ID 104) in the ground
                elseif groundId == 9045 then
                    Game.createItem(105, 1, target:getPosition()) -- Create a hole (ID 105) in the ground
                end
            end
        end

        return false
    end

    player:say("Digging... This will take " .. shovel.dig_time .. " seconds.", TALKTYPE_MONSTER_SAY)

    local targetPos = toPosition or target:getPosition()
    targetPos:sendMagicEffect(CONST_ME_POFF)

    -- Block player movement during digging
    player:setMovementBlocked(true)

    addEvent(function()
        targetPos:sendMagicEffect(CONST_ME_HITAREA)

        if shovel.expiration and shovel.expiration < os.time() then
            player:say("Time's up! Nothing was found.", TALKTYPE_MONSTER_SAY)
        else
            if math.random() <= shovel.find_chance then
                local randomItem = shovel.possible_items[math.random(#shovel.possible_items)]
                player:addItem(randomItem.itemid, randomItem.count)
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You found an item while digging!")
            else
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You dug but found nothing.")
            end

            -- Decrement shovel uses and check if broken
            shovel.uses = shovel.uses - 1
            if shovel.uses <= 0 then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "Your shovel broke.")
                item:remove(1)
                
                local tile = Tile(targetPos)
                if tile then
                    local ground = tile:getGround()
                    if ground then
                        local groundId = ground:getId()
                        if groundId == 9043 then
                            Game.createItem(103, 1, targetPos) -- Create a hole (ID 103) in the ground
                        elseif groundId == 9044 then
                            Game.createItem(104, 1, targetPos) -- Create a hole (ID 104) in the ground
                        elseif groundId == 9045 then
                            Game.createItem(105, 1, targetPos) -- Create a hole (ID 105) in the ground
                        end
                    end
                end
            end

            -- Release player movement after digging
            player:setMovementBlocked(false)
            diggingInProgress[player:getGuid()] = nil
        end
    end, shovel.dig_time * 1000)

    return true
end
 
Last edited:
Not tested!
Lua:
-- Definition of shovel properties
local shovels = {
    [12345] = { -- Wooden Shovel ID
        dig_time = 5,  -- Digging time in seconds
        uses = 5,  -- Number of uses
        find_chance = 0.1,  -- Chance of finding an item (10%)
        possible_items = {2400, 2401, 2402}  -- Possible item IDs
    },
    [12346] = { -- Metal Shovel ID
        dig_time = 3,
        uses = 20,
        find_chance = 0.15,  -- Chance of finding an item (15%)
        possible_items = {2403, 2404, 2405}  -- Possible item IDs
    },
    [12347] = { -- Golden Shovel ID
        dig_time = 0.5,
        uses = math.huge,  -- Unlimited usage for one week
        find_chance = 0.25,  -- Chance of finding an item (25%)
        expiration = os.time() + (7 * 24 * 60 * 60),  -- Expires in one week
        possible_items = {2406, 2407, 2408}  -- Possible item IDs
    }
}

function findItem(possible_items)
    local items = possible_items
    local randomIndex = math.random(1, #items)
    return items[randomIndex]
end

function onUse(player, shovel_id, target)
    local shovel = shovels[shovel_id]
    if not shovel then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Invalid shovel type.")
        return false
    end

    -- Check if the golden shovel has expired
    if shovel_id == 12347 and os.time() > shovel.expiration then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Your golden shovel has expired.")
        return false
    end

    -- Check if the target is diggable and has the correct ActionID
    if not (target.itemid == 19512 and target.actionid == 9000) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot dig here.")
        return false
    end

    -- Inform the player about the digging process
    player:sendTextMessage(MESSAGE_INFO_DESCR, "Digging... This will take " .. shovel.dig_time .. " seconds.")
    addEvent(function()
        target:remove()

        -- Chance of finding something
        if math.random() < shovel.find_chance then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You found something!")
            local itemFound = findItem(shovel.possible_items)
            player:addItem(itemFound)
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You found nothing.")
        end

        -- Decrement shovel usage and check if it broke
        if shovel_id ~= 12347 then
            shovel.uses = shovel.uses - 1
            if shovel.uses <= 0 then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "Your shovel broke.")
            end
        end
    end, shovel.dig_time * 1000)

    return true
end
Holly shit gonna try and let you know if it works
Post automatically merged:

Not tested!
Lua:
-- Definition of shovel properties
local shovels = {
    [12345] = { -- Wooden Shovel ID
        dig_time = 5,  -- Digging time in seconds
        uses = 5,  -- Number of uses
        find_chance = 0.1,  -- Chance of finding an item (10%)
        possible_items = {2400, 2401, 2402}  -- Possible item IDs
    },
    [12346] = { -- Metal Shovel ID
        dig_time = 3,
        uses = 20,
        find_chance = 0.15,  -- Chance of finding an item (15%)
        possible_items = {2403, 2404, 2405}  -- Possible item IDs
    },
    [12347] = { -- Golden Shovel ID
        dig_time = 0.5,
        uses = math.huge,  -- Unlimited usage for one week
        find_chance = 0.25,  -- Chance of finding an item (25%)
        expiration = os.time() + (7 * 24 * 60 * 60),  -- Expires in one week
        possible_items = {2406, 2407, 2408}  -- Possible item IDs
    }
}

function findItem(possible_items)
    local items = possible_items
    local randomIndex = math.random(1, #items)
    return items[randomIndex]
end

function onUse(player, shovel_id, target)
    local shovel = shovels[shovel_id]
    if not shovel then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Invalid shovel type.")
        return false
    end

    -- Check if the golden shovel has expired
    if shovel_id == 12347 and os.time() > shovel.expiration then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Your golden shovel has expired.")
        return false
    end

    -- Check if the target is diggable and has the correct ActionID
    if not (target.itemid == 19512 and target.actionid == 9000) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot dig here.")
        return false
    end

    -- Inform the player about the digging process
    player:sendTextMessage(MESSAGE_INFO_DESCR, "Digging... This will take " .. shovel.dig_time .. " seconds.")
    addEvent(function()
        target:remove()

        -- Chance of finding something
        if math.random() < shovel.find_chance then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You found something!")
            local itemFound = findItem(shovel.possible_items)
            player:addItem(itemFound)
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You found nothing.")
        end

        -- Decrement shovel usage and check if it broke
        if shovel_id ~= 12347 then
            shovel.uses = shovel.uses - 1
            if shovel.uses <= 0 then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "Your shovel broke.")
            end
        end
    end, shovel.dig_time * 1000)

    return true
end
Okay i tested it always says Invalid shovel type. even tho its correct shovel
 
Last edited:
I fixed the edited post. Take a look.
Works perfectly but found two issues with it.
1. It doesnt remove the dig hole
2. After using 5 digs on the shovel, even if you craft new one it still says your shovel is broken
3. You can run away when you're digging it could not allow moving when you doing this action
4. If the shovel has 5 digs it and you use 5 of them it doesnt remove the shovel at that exact moment it dissapears only if you try to dig with the broken shovel again
 
The script has been corrected. It could have included information beforehand to know how to create the script. If the script doesn't meet your needs, I hope someone can correct it, and I would appreciate it.
 
The script has been corrected. It could have included information beforehand to know how to create the script. If the script doesn't meet your needs, I hope someone can correct it, and I would appreciate it.
It is indeed better now but still has the issue with shovels when you use those all uses = 5 and it deletes the shovel, after that if you create new shovel it always says broken. And it doesnt remove the hole sprite item when u dig it that has action id 9000, after using shovel on it once it should remove that item with actionid 9000. And -- Create a hole in the ground if the shovel is broken is not needed at all, because it shouldnt create any item when shovel breaks it should delete the item that you use it on
 
Last edited:
@Mateus Robeerto Your script shovel uses in the config are global, to all. Meaning that everyone has the same uses for that shovel.... In my opinion, if you want "uses" on an item, just take advantange of custom attributes, you can store "uses" on the item itself.
 
Last edited:
custom attributes? what u mean
C++:
registerMethod("Item", "getCustomAttribute", LuaScriptInterface::luaItemGetCustomAttribute);
registerMethod("Item", "setCustomAttribute", LuaScriptInterface::luaItemSetCustomAttribute);
registerMethod("Item", "removeCustomAttribute", LuaScriptInterface::luaItemRemoveCustomAttribute);
 
C++:
registerMethod("Item", "getCustomAttribute", LuaScriptInterface::luaItemGetCustomAttribute);
registerMethod("Item", "setCustomAttribute", LuaScriptInterface::luaItemSetCustomAttribute);
registerMethod("Item", "removeCustomAttribute", LuaScriptInterface::luaItemRemoveCustomAttribute);
have no idea how to use them
 
I believe I managed to finish the script as suggested by Fiorda. I used 'custom attributes' to store the 'uses' directly on the item, indicating when it was used and the remaining amount. I also added a visibility feature and an effect. Note: I don't have sprites for the PA, so I added sprites similar to the PICK as an example. If you don't have PA sprites, you can look for Pokémon ones that are free, just grab them there and add them to your sprite. Alternatively, you can use 'CONST_ME_HITAREA' for a visual effect when striking... Watch the video to see how it works xD.




Lua:
local shovels = {
    [2556] = { -- Wooden Shovel
        dig_time = 5,  -- Digging time in seconds
        uses = 5,  -- Number of uses
        find_chance = 0.1,  -- Chance to find an item (10%)
        possible_items = {{itemid = 2400, count = 3}, {itemid = 2401, count = 2}, {itemid = 2402, count = 1}}  -- Possible items with count
    },
    [2432] = { -- Metal Shovel
        dig_time = 3,
        uses = 20,
        find_chance = 0.15,
        possible_items = {{itemid = 2403, count = 1}, {itemid = 2404, count = 1}, {itemid = 2405, count = 2}}
    },
    [38864] = { -- Golden Shovel
        dig_time = 0.5,
        uses = math.huge,  -- Unlimited usage for one week
        find_chance = 0.25,
        expiration = os.time() + (7 * 24 * 60 * 60),  -- Expires in one week
        possible_items = {{itemid = 2406, count = 2}, {itemid = 2407, count = 1}, {itemid = 2408, count = 3}}
    }
}

local diggingInProgress = {}

function createHole(position)
    local tile = Tile(position)
    if tile then
        local ground = tile:getGround()
        if ground then
            local groundId = ground:getId()
            if groundId == 9043 then
                Game.createItem(103, 1, position) -- Create a hole (ID 103) in the ground
            elseif groundId == 9044 then
                Game.createItem(104, 1, position) -- Create a hole (ID 104) in the ground
            elseif groundId == 9045 then
                Game.createItem(105, 1, position) -- Create a hole (ID 105) in the ground
            end
        end
    end
end

function repeatEffect(position, effect, times)
    if times > 0 then
        position:sendMagicEffect(effect)
        addEvent(repeatEffect, 1000, position, effect, times - 1)
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    -- Check if the player is already digging
    if diggingInProgress[player:getGuid()] then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are already digging. Please wait until it's finished.")
        return false
    end

    local shovel = shovels[item:getId()]
    if not shovel then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You need to use a shovel to dig here.")
        return false
    end

    if target.actionid ~= 9000 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot dig here.")
        return false
    end

    local usesLeft = item:getCustomAttribute("uses") or shovel.uses
    if usesLeft <= 0 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your shovel is broken.")
        item:remove(1)
        return false
    else
        player:say("Digging... This will take " .. shovel.dig_time .. " seconds. Uses left: " .. usesLeft, TALKTYPE_MONSTER_SAY)
    end

    local targetPos = toPosition or target:getPosition()
    targetPos:sendMagicEffect(CONST_ME_POFF)
    player:setMovementBlocked(true)

    -- Repeat the effect CONST_ME_HITAREA during the digging time
    repeatEffect(targetPos, CONST_ME_HITAREA, shovel.dig_time)

    addEvent(function()
        targetPos:sendMagicEffect(CONST_ME_HITAREA)

        usesLeft = usesLeft - 1
        if usesLeft <= 0 then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Your shovel broke.")
            item:remove(1)
            createHole(target:getPosition()) -- Create a hole in the ground when the shovel breaks
        else
            item:setCustomAttribute("uses", usesLeft)
            if math.random() <= shovel.find_chance then
                local randomItem = shovel.possible_items[math.random(#shovel.possible_items)]
                player:addItem(randomItem.itemid, randomItem.count)
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You found an item while digging!")
            else
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You dug but found nothing.")
            end
        end

        player:setMovementBlocked(false)
        diggingInProgress[player:getGuid()] = nil
    end, shovel.dig_time * 1000)

    diggingInProgress[player:getGuid()] = true
    return true
end
 
Solution
I believe I managed to finish the script as suggested by Fiorda. I used 'custom attributes' to store the 'uses' directly on the item, indicating when it was used and the remaining amount. I also added a visibility feature and an effect. Note: I don't have sprites for the PA, so I added sprites similar to the PICK as an example. If you don't have PA sprites, you can look for Pokémon ones that are free, just grab them there and add them to your sprite. Alternatively, you can use 'CONST_ME_HITAREA' for a visual effect when striking... Watch the video to see how it works xD.




Lua:
local shovels = {
    [2556] = { -- Wooden Shovel
        dig_time = 5,  -- Digging time in seconds
        uses = 5,  -- Number of uses
        find_chance = 0.1,  -- Chance to find an item (10%)
        possible_items = {{itemid = 2400, count = 3}, {itemid = 2401, count = 2}, {itemid = 2402, count = 1}}  -- Possible items with count
    },
    [2432] = { -- Metal Shovel
        dig_time = 3,
        uses = 20,
        find_chance = 0.15,
        possible_items = {{itemid = 2403, count = 1}, {itemid = 2404, count = 1}, {itemid = 2405, count = 2}}
    },
    [38864] = { -- Golden Shovel
        dig_time = 0.5,
        uses = math.huge,  -- Unlimited usage for one week
        find_chance = 0.25,
        expiration = os.time() + (7 * 24 * 60 * 60),  -- Expires in one week
        possible_items = {{itemid = 2406, count = 2}, {itemid = 2407, count = 1}, {itemid = 2408, count = 3}}
    }
}

local diggingInProgress = {}

function createHole(position)
    local tile = Tile(position)
    if tile then
        local ground = tile:getGround()
        if ground then
            local groundId = ground:getId()
            if groundId == 9043 then
                Game.createItem(103, 1, position) -- Create a hole (ID 103) in the ground
            elseif groundId == 9044 then
                Game.createItem(104, 1, position) -- Create a hole (ID 104) in the ground
            elseif groundId == 9045 then
                Game.createItem(105, 1, position) -- Create a hole (ID 105) in the ground
            end
        end
    end
end

function repeatEffect(position, effect, times)
    if times > 0 then
        position:sendMagicEffect(effect)
        addEvent(repeatEffect, 1000, position, effect, times - 1)
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    -- Check if the player is already digging
    if diggingInProgress[player:getGuid()] then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are already digging. Please wait until it's finished.")
        return false
    end

    local shovel = shovels[item:getId()]
    if not shovel then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You need to use a shovel to dig here.")
        return false
    end

    if target.actionid ~= 9000 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot dig here.")
        return false
    end

    local usesLeft = item:getCustomAttribute("uses") or shovel.uses
    if usesLeft <= 0 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your shovel is broken.")
        item:remove(1)
        return false
    else
        player:say("Digging... This will take " .. shovel.dig_time .. " seconds. Uses left: " .. usesLeft, TALKTYPE_MONSTER_SAY)
    end

    local targetPos = toPosition or target:getPosition()
    targetPos:sendMagicEffect(CONST_ME_POFF)
    player:setMovementBlocked(true)

    -- Repeat the effect CONST_ME_HITAREA during the digging time
    repeatEffect(targetPos, CONST_ME_HITAREA, shovel.dig_time)

    addEvent(function()
        targetPos:sendMagicEffect(CONST_ME_HITAREA)

        usesLeft = usesLeft - 1
        if usesLeft <= 0 then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Your shovel broke.")
            item:remove(1)
            createHole(target:getPosition()) -- Create a hole in the ground when the shovel breaks
        else
            item:setCustomAttribute("uses", usesLeft)
            if math.random() <= shovel.find_chance then
                local randomItem = shovel.possible_items[math.random(#shovel.possible_items)]
                player:addItem(randomItem.itemid, randomItem.count)
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You found an item while digging!")
            else
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You dug but found nothing.")
            end
        end

        player:setMovementBlocked(false)
        diggingInProgress[player:getGuid()] = nil
    end, shovel.dig_time * 1000)

    diggingInProgress[player:getGuid()] = true
    return true
end
Works perfectly <3
 
I believe I managed to finish the script as suggested by Fiorda. I used 'custom attributes' to store the 'uses' directly on the item, indicating when it was used and the remaining amount. I also added a visibility feature and an effect. Note: I don't have sprites for the PA, so I added sprites similar to the PICK as an example. If you don't have PA sprites, you can look for Pokémon ones that are free, just grab them there and add them to your sprite. Alternatively, you can use 'CONST_ME_HITAREA' for a visual effect when striking... Watch the video to see how it works xD.




Lua:
local shovels = {
    [2556] = { -- Wooden Shovel
        dig_time = 5,  -- Digging time in seconds
        uses = 5,  -- Number of uses
        find_chance = 0.1,  -- Chance to find an item (10%)
        possible_items = {{itemid = 2400, count = 3}, {itemid = 2401, count = 2}, {itemid = 2402, count = 1}}  -- Possible items with count
    },
    [2432] = { -- Metal Shovel
        dig_time = 3,
        uses = 20,
        find_chance = 0.15,
        possible_items = {{itemid = 2403, count = 1}, {itemid = 2404, count = 1}, {itemid = 2405, count = 2}}
    },
    [38864] = { -- Golden Shovel
        dig_time = 0.5,
        uses = math.huge,  -- Unlimited usage for one week
        find_chance = 0.25,
        expiration = os.time() + (7 * 24 * 60 * 60),  -- Expires in one week
        possible_items = {{itemid = 2406, count = 2}, {itemid = 2407, count = 1}, {itemid = 2408, count = 3}}
    }
}

local diggingInProgress = {}

function createHole(position)
    local tile = Tile(position)
    if tile then
        local ground = tile:getGround()
        if ground then
            local groundId = ground:getId()
            if groundId == 9043 then
                Game.createItem(103, 1, position) -- Create a hole (ID 103) in the ground
            elseif groundId == 9044 then
                Game.createItem(104, 1, position) -- Create a hole (ID 104) in the ground
            elseif groundId == 9045 then
                Game.createItem(105, 1, position) -- Create a hole (ID 105) in the ground
            end
        end
    end
end

function repeatEffect(position, effect, times)
    if times > 0 then
        position:sendMagicEffect(effect)
        addEvent(repeatEffect, 1000, position, effect, times - 1)
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    -- Check if the player is already digging
    if diggingInProgress[player:getGuid()] then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are already digging. Please wait until it's finished.")
        return false
    end

    local shovel = shovels[item:getId()]
    if not shovel then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You need to use a shovel to dig here.")
        return false
    end

    if target.actionid ~= 9000 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot dig here.")
        return false
    end

    local usesLeft = item:getCustomAttribute("uses") or shovel.uses
    if usesLeft <= 0 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your shovel is broken.")
        item:remove(1)
        return false
    else
        player:say("Digging... This will take " .. shovel.dig_time .. " seconds. Uses left: " .. usesLeft, TALKTYPE_MONSTER_SAY)
    end

    local targetPos = toPosition or target:getPosition()
    targetPos:sendMagicEffect(CONST_ME_POFF)
    player:setMovementBlocked(true)

    -- Repeat the effect CONST_ME_HITAREA during the digging time
    repeatEffect(targetPos, CONST_ME_HITAREA, shovel.dig_time)

    addEvent(function()
        targetPos:sendMagicEffect(CONST_ME_HITAREA)

        usesLeft = usesLeft - 1
        if usesLeft <= 0 then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Your shovel broke.")
            item:remove(1)
            createHole(target:getPosition()) -- Create a hole in the ground when the shovel breaks
        else
            item:setCustomAttribute("uses", usesLeft)
            if math.random() <= shovel.find_chance then
                local randomItem = shovel.possible_items[math.random(#shovel.possible_items)]
                player:addItem(randomItem.itemid, randomItem.count)
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You found an item while digging!")
            else
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You dug but found nothing.")
            end
        end

        player:setMovementBlocked(false)
        diggingInProgress[player:getGuid()] = nil
    end, shovel.dig_time * 1000)

    diggingInProgress[player:getGuid()] = true
    return true
end
Hey bro,
this is not revscript ? :))
 
Back
Top