• 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 Script "auto use next exercise weapon" [TSF 1.3] Server 13.11

Kyubinhok

New Member
Joined
Nov 15, 2021
Messages
2
Reaction score
0
GitHub
Kyubinhok
Hi guys, I need help with a script about [Using some special item example.. ID 17112, Enables the automatic next exercise weapon. (If player have another exactly same ID exercise.. example: When finish the last charge of the item: " You see an lasting exercise bow that has 14400 charges left. (ID: 35288), If i have another item with the same ID, the character continue using the charges of the next item with the same id..

The item mentioned is only to use once time... to modify character and gain that "ability forever"

I have two scripts in my server files:

First script located in: data/libs/exercise_training.lua
Lua:
ExerciseWeaponsTable = {
    -- MELE
    [28540] = { skill = SKILL_SWORD },
    [28552] = { skill = SKILL_SWORD },
    [35279] = { skill = SKILL_SWORD },
    [35285] = { skill = SKILL_SWORD },
    [28553] = { skill = SKILL_AXE },
    [28541] = { skill = SKILL_AXE },
    [35280] = { skill = SKILL_AXE },
    [35286] = { skill = SKILL_AXE },
    [28554] = { skill = SKILL_CLUB },
    [28542] = { skill = SKILL_CLUB },
    [35281] = { skill = SKILL_CLUB },
    [35287] = { skill = SKILL_CLUB },
    [23283] = { skill = SKILL_FIST },
    [33333] = { skill = SKILL_FIST },
    [25634] = { skill = SKILL_FIST },
    [33332] = { skill = SKILL_FIST },
    -- ROD
    [28544] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_SMALLICE, allowFarUse = true },
    [28556] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_SMALLICE, allowFarUse = true },
    [35283] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_SMALLICE, allowFarUse = true },
    [35289] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_SMALLICE, allowFarUse = true },
    -- RANGE
    [28543] = { skill = SKILL_DISTANCE, effect = CONST_ANI_SIMPLEARROW, allowFarUse = true },
    [28555] = { skill = SKILL_DISTANCE, effect = CONST_ANI_SIMPLEARROW, allowFarUse = true },
    [35282] = { skill = SKILL_DISTANCE, effect = CONST_ANI_SIMPLEARROW, allowFarUse = true },
    [35288] = { skill = SKILL_DISTANCE, effect = CONST_ANI_SIMPLEARROW, allowFarUse = true },
    -- WAND
    [28545] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_FIRE, allowFarUse = true },
    [28557] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_FIRE, allowFarUse = true },
    [35284] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_FIRE, allowFarUse = true },
    [35290] = { skill = SKILL_MAGLEVEL, effect = CONST_ANI_FIRE, allowFarUse = true }
}

FreeDummies = {28558, 28565}
MaxAllowedOnADummy = configManager.getNumber(configKeys.MAX_ALLOWED_ON_A_DUMMY)
HouseDummies = {28559, 28560, 28561, 28562, 28563, 28564}

local magicLevelRate = configManager.getNumber(configKeys.RATE_MAGIC)
local skillLevelRate = configManager.getNumber(configKeys.RATE_SKILL)

function LeaveTraining(playerId)
    if onExerciseTraining[playerId] then
        stopEvent(onExerciseTraining[playerId].event)
        onExerciseTraining[playerId] = nil
    end

    local player = Player(playerId)
    if player then
        player:setTraining(false)
    end
    return
end

function ExerciseEvent(playerId, tilePosition, weaponId, dummyId)
    local player = Player(playerId)
    if not player then
        return LeaveTraining(playerId)
    end

    if player:isTraining() == 0 then
        return LeaveTraining(playerId)
    end

    if not Tile(tilePosition):getItemById(dummyId) then
        player:sendTextMessage(MESSAGE_FAILURE, "Someone has moved the dummy, the training has stopped.")
        LeaveTraining(playerId)
        return false
    end

    local playerPosition = player:getPosition()
    if not playerPosition:isProtectionZoneTile() then
        player:sendTextMessage(MESSAGE_FAILURE, "You are no longer in a protection zone, the training has stopped.")
        LeaveTraining(playerId)
        return false
    end

    if player:getItemCount(weaponId) <= 0 then
        player:sendTextMessage(MESSAGE_FAILURE, "You need the training weapon in the backpack, the training has stopped.")
        LeaveTraining(playerId)
        return false
    end

    local weapon = player:getItemById(weaponId, true)
    if not weapon:isItem() or not weapon:hasAttribute(ITEM_ATTRIBUTE_CHARGES) then
        player:sendTextMessage(MESSAGE_FAILURE, "The selected item is not a training weapon, the training has stopped.")
        LeaveTraining(playerId)
        return false
    end

    local weaponCharges = weapon:getAttribute(ITEM_ATTRIBUTE_CHARGES)
    if not weaponCharges or weaponCharges <= 0 then
        weapon:remove(1) -- ??
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your training weapon has disappeared.")
        LeaveTraining(playerId)
        return false
    end

    local isMagic = ExerciseWeaponsTable[weaponId].skill == SKILL_MAGLEVEL
    local bonusDummy = table.contains(HouseDummies, dummyId) or nil

    if bonusDummy then bonusDummy = 1.1 else bonusDummy = 1 end

    if isMagic then
        player:addManaSpent(500 * bonusDummy)
    else
        player:addSkillTries(ExerciseWeaponsTable[weaponId].skill, 7 * bonusDummy)
    end

    weapon:setAttribute(ITEM_ATTRIBUTE_CHARGES, (weaponCharges - 1))
    tilePosition:sendMagicEffect(CONST_ME_HITAREA)

    if ExerciseWeaponsTable[weaponId].effect then
        playerPosition:sendDistanceEffect(tilePosition, ExerciseWeaponsTable[weaponId].effect)
    end

if weapon:getAttribute(ITEM_ATTRIBUTE_CHARGES) <= 0 then
    weapon:remove(1)
    local weapon = player:getItemById(weaponId, true)
    if not weapon or (not weapon:isItem() or not weapon:hasAttribute(ITEM_ATTRIBUTE_CHARGES)) then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your training weapon has disappeared.")
        LeaveTraining(playerId)
        return false
    end
end

    local vocation = player:getVocation()
    onExerciseTraining[playerId].event = addEvent(ExerciseEvent, vocation:getBaseAttackSpeed() / configManager.getFloat(configKeys.RATE_EXERCISE_TRAINING_SPEED), playerId, tilePosition, weaponId, dummyId)
    return true
end

Second script located in: scripts/actions/other/exercise_training.lua
Code:
local exerciseTraining = Action()
function exerciseTraining.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local playerId = player:getId()
    local targetId = target:getId()
    if target:isItem() and (table.contains(HouseDummies, targetId) or table.contains(FreeDummies, targetId)) then
        if onExerciseTraining[playerId] then
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "This exercise dummy can only be used after a 30 second cooldown.")
            LeaveTraining(playerId)
            return true
        end
        local playerPos = player:getPosition()
        if not ExerciseWeaponsTable[item.itemid].allowFarUse and (playerPos:getDistance(target:getPosition()) > 1) then
            player:sendTextMessage(MESSAGE_FAILURE, "Get closer to the dummy.")
            return true
        end
        if not playerPos:isProtectionZoneTile() then
            player:sendTextMessage(MESSAGE_FAILURE, "You need to be in a protection zone.")
            return true
        end
        local playerHouse = player:getTile():getHouse()
        local targetPos = target:getPosition()
        local targetHouse = Tile(targetPos):getHouse()

        if table.contains(HouseDummies, targetId) then
            if playerHouse ~= targetHouse then
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You must be inside the house to use this dummy.")
                return true
            end
            local playersOnDummy = 0
            for _, playerTraining in pairs(onExerciseTraining) do
                if playerTraining.dummyPos == targetPos then
                    playersOnDummy = playersOnDummy + 1
                end
                if playersOnDummy == MaxAllowedOnADummy then
                    player:sendTextMessage(MESSAGE_FAILURE, "That exercise dummy is busy.")
                    return true
                end
            end
        end

        if player:getStorageValue(Storage.isTraining) > os.time() then
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "This exercise dummy can only be used after a 30 second cooldown.")
            return true
        end
        onExerciseTraining[playerId] = {}
        if not onExerciseTraining[playerId].event then
            onExerciseTraining[playerId].event = addEvent(ExerciseEvent, 0, playerId, targetPos, item.itemid, targetId)
            onExerciseTraining[playerId].dummyPos = targetPos
            player:setTraining(true)
            player:setStorageValue(Storage.isTraining, os.time() + 30)
        end
        return true
    end
    return false
end
for weaponId, weapon in pairs(ExerciseWeaponsTable) do
    exerciseTraining:id(weaponId)
    if weapon.allowFarUse then
        exerciseTraining:allowFarUse(true)
    end
end
exerciseTraining:register()

Please help me, I am new on this.. and will help me a lot..
Thank you in advance! <3
 
Back
Top