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

[TFS 1.3] Reward based on vocation

krafttomten

Member
Joined
Jul 23, 2017
Messages
25
Reaction score
5
Hello,

I have been stuck on this problem for too long now. I give up! Time to admit defeat and beg the gods of OTland for help.

Currently, the code I have gives the player the content of the chest as reward. But sometimes I want to give different rewards for different vocations. I have found similar threads on the subject, but the solutions I have found all rely on adding the item ID:s straight into the code, which basically means that I would need one script file for each quest. No good. I want standardized code for all quests, so much easier in the long run.

I'm thinking that this code only needs a small fix really, but I can't get my head around the LUA syntax to formulate the task properly.

Lua:
function onUse(cid, item, fromPosition, target, toPosition, isHotkey)
    local chest = Container(item.uid) --
    local player = Player(cid)
    if not chest or not player then
      return true
    end

    local uniqueid = chest:getUniqueId()
    if player:getStorageValue(uniqueid) == -2 then
      player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
      return true
    end

    local start = player:getStorageValue(uniqueid) == -1 and 0 or player:getStorageValue(uniqueid)
    for i = start, chest:getSize() do
      local reward = chest:getItem(i) -- Here, instead of adding the chest content to the reward variable,
      if not reward then              -- I want it to add the first item in the chest to vocation 1,
        break                         -- the second item to vocation 2, etc...
      end

      local rewardWeight = reward.getWeight and reward:getWeight() or ItemType(reward:getId()):getWeight(reward:getCount())
      if rewardWeight > player:getFreeCapacity() then
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. rewardWeight/100 .. ' oz it\'s too heavy.')
        player:setStorageValue(uniqueid, i)
        break
      else
        reward = reward:clone()
        if player:addItemEx(reward) ~= RETURNVALUE_NOERROR then
          player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. rewardWeight/100 .. ' oz it\'s too heavy.')
          break
        end

        local reward_msg = reward:getArticle() .. ' ' .. reward:getName()
        if reward:getCount() > 1 then
          reward_msg = reward:getCount() .. ' ' .. reward:getPluralName()
        end

        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found ' .. reward_msg .. '.')

        player:setStorageValue(uniqueid, -2)
      end
    end

    return true
end

I've been trying to combine container:getItem(index) with player:getVocation() and match them up that way but I don't get it to work.
Lua:
item:getItem(player:getVocation())

If I get this to work then all l I would have to do to make a new quest would be to make a chest in RME, give it an actionid that runs this code and add the reward items inside the chest (in the correct order).

Any help would be appriciated!
Post automatically merged:

Posting progress so far.

Lua:
function onUse(cid, item, fromPosition, target, toPosition, isHotkey)
    local chest = Container(item.uid)
    local player = Player(cid)
   


    local aids = { -- I'm trying to assign each number to one vocation (by index) here
        [1] = 13001, --
        [2] = 13002, --
        [3] = 13003, --
        [4] = 13004 --
    }




    if not chest or not player then
      return true
    end

    local uniqueid = chest:getUniqueId()
    if player:getStorageValue(uniqueid) == -2 then
      player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
      return true
    end
   
    local start = player:getStorageValue(uniqueid) == -1 and 0 or player:getStorageValue(uniqueid)
    for i = start, chest:getSize() do
    if item.aid == aids.getPlayerVocation(cid) or item.aid == aids:getPlayerVocation(cid - 4)  then -- This is where it breaks. I cannot
        local reward = chest:getItem(i)             -- call aids.getPlayerVocation(cid) because it is a Nil value. I'm trying to match it with
      if not reward then              -- the corresponding aid above (aids)
        break
      end

      local rewardWeight = reward.getWeight and reward:getWeight() or ItemType(reward:getId()):getWeight(reward:getCount())
      if rewardWeight > player:getFreeCapacity() then
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. rewardWeight/100 .. ' oz it\'s too heavy.')
        player:setStorageValue(uniqueid, i)
        break
      else
        reward = reward:clone()
        if player:addItemEx(reward) ~= RETURNVALUE_NOERROR then
          player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. rewardWeight/100 .. ' oz it\'s too heavy.')
          break
        end

        local reward_msg = reward:getArticle() .. ' ' .. reward:getName()
        if reward:getCount() > 1 then
          reward_msg = reward:getCount() .. ' ' .. reward:getPluralName()
        end

        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found ' .. reward_msg .. '.')

        player:setStorageValue(uniqueid, -2)
      end
    end
    end

    return true
end

I'm trying a list of action ID's now. That way I can add many items to each vocation and it won't matter what order I place them inside the map editor
 
Last edited:
Solution
data/scripts/rewardchests.lua
Lua:
local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    if not item:getType():isContainer() then
        return true
    end

    local uniqueId = item:getUniqueId()
    if player:getStorageValue(uniqueId) ~= -1 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local vocation = player:getVocation():getBase()
    local get = item:getItem(vocation:getId() -1)
    if get then
        local weight = get:getWeight()
        if player:getFreeCapacity() < weight then
            player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("You need %.2f weight.", weight / 100))
            return true...

Sarah Wesker

ค∂vαηcε รүηтαx ❤
Support Team
Joined
Mar 16, 2017
Messages
997
Solutions
100
Reaction score
1,033
Location
London
GitHub
MillhioreBT
data/scripts/rewardchests.lua
Lua:
local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    if not item:getType():isContainer() then
        return true
    end

    local uniqueId = item:getUniqueId()
    if player:getStorageValue(uniqueId) ~= -1 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local vocation = player:getVocation():getBase()
    local get = item:getItem(vocation:getId() -1)
    if get then
        local weight = get:getWeight()
        if player:getFreeCapacity() < weight then
            player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("You need %.2f weight.", weight / 100))
            return true
        end
        local reward = get:clone()
        if player:addItemEx(reward) == RETURNVALUE_NOERROR then
            player:setStorageValue(uniqueId, 1)
            if reward:getType():isContainer() then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "You found: " .. reward:getContentDescription())
            else
                player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("You found: %d %s.", reward:getCount(), reward:getName()))
            end
        end
    end
    return true
end

action:aid(7776)
action:register()

then the reward is listed as follows:
  • Sorcerer
  • Druid
  • Paladin
  • Knight
  • Custom Voc "Example"
1618875911718.png
 
Last edited:
Solution

adrian alvarez

New Member
Joined
Mar 7, 2017
Messages
34
Solutions
1
Reaction score
3
data/scripts/rewardchests.lua
Lua:
local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    if not item:getType():isContainer() then
        return true
    end

    local uniqueId = item:getUniqueId()
    if player:getStorageValue(uniqueId) ~= -1 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local vocation = player:getVocation():getBase()
    local get = item:getItem(vocation:getId() -1)
    if get then
        local weight = get:getWeight()
        if player:getFreeCapacity() < weight then
            player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("You need %.2f weight.", weight / 100))
            return true
        end
        local reward = get:clone()
        if player:addItemEx(reward) == RETURNVALUE_NOERROR then
            player:setStorageValue(uniqueId, 1)
            if reward:getType():isContainer() then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "You found: " .. reward:getContentDescription())
            else
                player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("You found: %d %s.", reward:getCount(), reward:getName()))
            end
        end
    end
    return true
end

action:aid(7776)
action:register()

then the reward is listed as follows:
  • Sorcerer
  • Druid
  • Paladin
  • Knight
  • Custom Voc "Example"
View attachment 57837
i get this error
/data/scripts/chestforvocation.lua:4: 'then' expected near 'isContainer'
 
Top