• 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 Learn spells from chest

Lopaskurwa

Active Member
Joined
Oct 6, 2017
Messages
870
Solutions
2
Reaction score
49
TFS 1.2
Hello,
have a 3 spells and i would like to make them as a reward in chest when you click on it but you can choose only one spell from 3 chests. So basically you can learn only one spell. Btw probably i would need to edit my spells too isnt it so it wont allow to use it until you didnt learned them?
 
Solution
A
Lua:
local spellsChest = {
    [10001] = {spell = "", vocation = {1, 2}},
    [10002] = {spell = "", vocation = {3}},
    [10003] = {spell = "", vocation = {4}},
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not spellsChest[item:getActionId()] then return true end
    local spellName = spellsChest[item:getActionId()].spell
    local spellVocation = spellsChest[item:getActionId()].vocation
    
    if (player:getStorageValue(25454) == 1) then
        player:sendCancelMessage("Sorry, you have already used this chest.")
        return true
    end
    
    if isInArray(spellVocation, player:getVocation():getId()) then
        if player:hasLearnedSpell(spellName) then
            player:learnSpell(spellName)...
Lua:
local spellsChest = {
    [10001] = {spell = "", vocation = {1, 2}},
    [10002] = {spell = "", vocation = {3}},
    [10003] = {spell = "", vocation = {4}},
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not spellsChest[item:getActionId()] then return true end
    local spellName = spellsChest[item:getActionId()].spell
    local spellVocation = spellsChest[item:getActionId()].vocation
    
    if (player:getStorageValue(25454) == 1) then
        player:sendCancelMessage("Sorry, you have already used this chest.")
        return true
    end
    
    if isInArray(spellVocation, player:getVocation():getId()) then
        if player:hasLearnedSpell(spellName) then
            player:learnSpell(spellName)
            player:setStorageValue(25454, 1)
            player:say("You have learned "..spellName..".", TALKTYPE_MONSTER_SAY)
        else
            player:sendCancelMessage("Sorry, you already know this spell.")
        end
    else
        player:sendCancelMessage("Sorry, your vocation can't learn this spell.")
    end
return true
end
 
Last edited by a moderator:
Solution
Lua:
local spellsChest = {
    [10001] = {spell = "", vocation = {1, 2}},
    [10002] = {spell = "", vocation = {3}},
    [10003] = {spell = "", vocation = {4}},
}

local spellsChestAction = Action()
function spellsChestAction.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not spellsChest[item:getActionId()] then return true end
    local spellName = spellsChest[item:getActionId()].spell
    local spellVocation = spellsChest[item:getActionId()].vocation
  
    if isInArray(spellVocation, player:getVocation():getId()) then
        if player:hasLearnedSpell(spellName) then
            player:learnSpell(spellName)
            player:say("You have learned "..spellName..".", TALKTYPE_MONSTER_SAY)
        else
            player:sendCancelMessage("Sorry, you already know this spell.")
        end
    else
        player:sendCancelMessage("Sorry, your vocation can't learn this spell.")
    end
return true
end

spellsChestAction:aid(10001, 10002, 10003)
spellsChestAction:register()

Or just, if you aren't using revscripts
Lua:
local spellsChest = {
    [10001] = {spell = "", vocation = {1, 2}},
    [10002] = {spell = "", vocation = {3}},
    [10003] = {spell = "", vocation = {4}},
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not spellsChest[item:getActionId()] then return true end
    local spellName = spellsChest[item:getActionId()].spell
    local spellVocation = spellsChest[item:getActionId()].vocation
   
    if isInArray(spellVocation, player:getVocation():getId()) then
        if player:hasLearnedSpell(spellName) then
            player:learnSpell(spellName)
            player:say("You have learned "..spellName..".", TALKTYPE_MONSTER_SAY)
        else
            player:sendCancelMessage("Sorry, you already know this spell.")
        end
    else
        player:sendCancelMessage("Sorry, your vocation can't learn this spell.")
    end
return true
end
Something isnt right. So edited the code a little bit probably fucked it up :D Nothing happens opening chest. Register in action
Lua:
local spellsChest = {
    [2555] = {spell = "aram"},
    [2556] = {spell = "aram second"},
    [2557] = {spell = "aram third"},
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not spellsChest[item:getActionId()] then return true end
    local spellName = spellsChest[item:getActionId()].spell
    local spellVocation = spellsChest[item:getActionId()].vocation
    
    if isInArray(spellVocation) then
        if player:hasLearnedSpell(spellName) then
            player:learnSpell(spellName)
            player:say("You have learned "..spellName..".", TALKTYPE_MONSTER_SAY)
        else
            player:sendCancelMessage("Sorry, you already know this spell.")
        end
    end
return true
end
XML:
    <action actionid="2555" script="quests/spellchest.lua" />
 
Something isnt right. So edited the code a little bit probably fucked it up :D Nothing happens opening chest. Register in action
Lua:
local spellsChest = {
    [2555] = {spell = "aram"},
    [2556] = {spell = "aram second"},
    [2557] = {spell = "aram third"},
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not spellsChest[item:getActionId()] then return true end
    local spellName = spellsChest[item:getActionId()].spell
    local spellVocation = spellsChest[item:getActionId()].vocation
  
    if isInArray(spellVocation) then
        if player:hasLearnedSpell(spellName) then
            player:learnSpell(spellName)
            player:say("You have learned "..spellName..".", TALKTYPE_MONSTER_SAY)
        else
            player:sendCancelMessage("Sorry, you already know this spell.")
        end
    end
return true
end
XML:
    <action actionid="2555" script="quests/spellchest.lua" />
Show me the original setup and try to stay with the original script, as you removed the isInArray part, you also need to register 3 different actionids (for each chest with each different spell).

Edit: I've edited my own post to add the storage thing that i missed before, now it locks if you've already learned a spell
 
Back
Top