• 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 Item returning unexpected value

noshirtstan

New Member
Joined
Aug 2, 2020
Messages
68
Reaction score
2
Hey folks, I'm trying to get my recipe system working in crafting, but for some reason, some recipes are coming up as "You have already learned this". Although it does not appear in my Crafting MW.

TFS 1.3

Here is what I have:

Lua:
function Player.isProfession(self, storage)
   if self:getStorageValue(storage) >= 1 then
     return true
   end
end
craftingProfessionsConfig = {
   [50501] = {
     skillName = "Blacksmithing",
     skillRecipes = {
       [1] = {item = 26382, skill = 10, storage = 101, mats = {{26435, 5}, {26434, 2}, {26416, 2}}, time = 10, difficulty = 50},
       [2] = {item = 26383, skill = 10, storage = 102, mats = {{26436, 1}, {26431, 5}, {26432, 2}, {26415, 2}}, time = 10, difficulty = 50}, -- doesnt work
       [3] = {item = 26402, skill = 10, storage = 103, mats = {{26434, 1}, {26435, 2}, {26408, 3}}, time = 5, difficulty = 50}, -- doesnt work
       [4] = {item = 26400, skill = 10, storage = 104, mats = {{26435, 5}, {26416, 2}, {26408, 3}}, time = 5, difficulty = 50}, -- doesnt work
       [5] = {item = 26401, skill = 10, storage = 105, mats = {{26435, 3}, {26434, 2}, {26408, 2}}, time = 5, difficulty = 50}, -- doesnt work
       [6] = {item = 26399, skill = 10, storage = 106, mats = {{26435, 1}, {26434, 1}, {26416, 3}, {26414, 2}}, time = 5, difficulty = 50},
       [7] = {item = 26434, skill = 10, storage = 107, mats = {{26410, 3}, {26413, 1}}, time = 3, difficulty = 30},
       [8] = {item = 26435, skill = 10, storage = 108, mats = {{26411, 3}, {26413, 2}}, time = 3, difficulty = 30},
       [9] = {item = 26436, skill = 10, storage = 109, mats = {{26412, 5}, {26413, 3}}, time = 3, difficulty = 50},
       [10] = {item = 26437, skill = 10, storage = 110, mats = {{26413, 2}, {26411, 2}, {26412, 3}, {26413, 5}}, time = 3, difficulty = 50}
     },
     message = "Crafting allows you to create anything from potions to bombs to weapons of the highest caliber. The materials used in crafting can be obtained through gathering and through drops in dungeons and the open world.\n\n"
   },
   [50502] = {
     skillName = "Alchemy",
     skillRecipes = { --Based on Shinmaru/Soul4Soul Alchemy's System
       [1] = {item = 26385, skill = 10, storage = 201, mats = {{26439, 1}, {26441, 10}, {26444, 10}, {26440, 20}}, time = 10, difficulty = 30},
       [2] = {item = 26386, skill = 10, storage = 202, mats = {{26439, 1}, {26441, 15}, {26444, 15}, {26440, 25}}, time = 10, difficulty = 30},
       [3] = {item = 26387, skill = 10, storage = 203, mats = {{26439, 1}, {26441, 20}, {26444, 20}, {26440, 30}}, time = 10, difficulty = 30},
       [4] = {item = 26388, skill = 10, storage = 204, mats = {{26439, 1}, {26442, 10}, {26445, 10}, {26440, 20}}, time = 20, difficulty = 40},
       [5] = {item = 26389, skill = 10, storage = 205, mats = {{26439, 1}, {26442, 15}, {26445, 15}, {26440, 25}}, time = 20, difficulty = 50},
       [6] = {item = 26390, skill = 10, storage = 206, mats = {{26439, 1}, {26442, 20}, {26445, 20}, {26440, 30}}, time = 20, difficulty = 60},
       [7] = {item = 26391, skill = 10, storage = 207, mats = {{26439, 1}, {26443, 10}, {26446, 15}, {26440, 20}}, time = 30, difficulty = 70},
       [8] = {item = 26392, skill = 10, storage = 208, mats = {{26439, 1}, {26443, 15}, {26446, 15}, {26440, 30}}, time = 30, difficulty = 80},
       [9] = {item = 26393, skill = 10, storage = 209, mats = {{26439, 1}, {26443, 20}, {26446, 20}, {26440, 50}}, time = 30, difficulty = 90},
       [10] = {item = 26394, skill = 10, storage = 210, mats = {{26439, 1}, {26443, 30}, {26446, 30}, {26440, 50}}, time = 45, difficulty = 100},
       [11] = {item = 26398, skill = 10, storage = 211, mats = {{26441, 1}, {26444, 1}, {26440, 1}}, time = 1, difficulty = 10}
     },
     message = "Crafting allows you to create ~"
   },
   [50503] = {
     skillName = "Inscription",
     skillRecipes = {
     },
     message = "Crafting allows you to create ~"
   },
   [50504] = {
     skillName = "Tailoring",
     skillRecipes = {
     },
     message = "Crafting allows you to create ~"
   },
   [50505] = {
     skillName = "Leatherworking",
     skillRecipes = {
     },
     message = "Crafting allows you to create ~"
   },
   [50506] = {
     skillName = "Engineering",
     skillRecipes = {
     },
     message = "Crafting allows you to create ~"
   },
   maxSkill = 500,
   baseRecipeStorage = 50500,
   extraData = {},
}

Code:
function capAll(str)
    return (str:gsub("(%l)(%w*)", function(a,b) return string.upper(a)..b end))
end

function onUse(player, item, fromPosition, itemEx, toPosition)
   local found = 0
   local recipes = craftingProfessionsConfig[item.actionid].skillRecipes
   local modal = ModalWindow(item.actionid, ""..craftingProfessionsConfig[item.actionid].skillName.." (Crafting Skill: "..player:getCustomSkill(item.actionid).."/"..craftingProfessionsConfig.maxSkill..")", craftingProfessionsConfig[item.actionid].message)

   if item.itemid == 8046 and isInArray({50501, 50502, 50503, 50504, 50505, 50506}, item.actionid) then
     if getCreatureCondition(player, CONDITION_SPELLCOOLDOWN, 160) then --I don't really know how to use :getCreatureCondition, it never works for me.
       return player:sendCancelMessage("You are already crafting.")
     end

     if not player:isProfession(item.actionid) then
       return player:sendCancelMessage("You need to learn "..craftingProfessionsConfig[item.actionid].skillName.." before using this.")
     end

     for i = 1, #recipes do
       if player:getStorageValue(craftingProfessionsConfig.baseRecipeStorage + recipes[i].storage) == 1 then
         modal:addChoice(i, capAll(getItemName(recipes[i].item))--[[.." ["..recipes[i].skill.."]"]])
       end
     end

     craftingProfessionsConfig.extraData[player:getId()] = {
       lastPos = Item(item.uid):getPosition()
     }

     if modal:getChoiceCount() ~= 0 then
       modal:addButton(1, "Create")
       modal:setDefaultEnterButton(1)
       modal:addButton(2, "Exit")
       modal:setDefaultEscapeButton(2)
       modal:addButton(3, "Materials")
       modal:sendToPlayer(player)
     else
       player:sendCancelMessage("You need to learn some "..craftingProfessionsConfig[item.actionid].skillName.." recipes first.")
     end
   elseif item.itemid == 26384 and item.actionid >= craftingProfessionsConfig.baseRecipeStorage then
     for i = 1, #recipes do
       if recipes[i].storage == tonumber(Item(item.uid):getAttribute(ITEM_ATTRIBUTE_TEXT)) then
         found = i
       end
     end
     if found == 0 then return player:sendCancelMessage(RETURNVALUE_CANNOTUSETHISOBJECT) end
     if player:getStorageValue(craftingProfessionsConfig.baseRecipeStorage + recipes[found].storage) == -1 then
       if player:getCustomSkill(item.actionid) >= recipes[found].skill then
         player:setStorageValue(craftingProfessionsConfig.baseRecipeStorage + recipes[found].storage, 1)
         player:sendTextMessage(MESSAGE_EVENT_DEFAULT, "You learned how to make "..capAll(getItemName(recipes[found].item))..".")
         player:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
         Item(item.uid):remove(1)
       else
         player:sendCancelMessage("You require "..recipes[found].skill.." crafting skill in "..craftingProfessionsConfig[item.actionid].skillName.." to learn this recipe.")
       end
     else
       player:sendCancelMessage("You have already learned this recipe.")
     end
   elseif item.itemid == 2217 and item.actionid >= craftingProfessionsConfig.baseRecipeStorage then
     if player:getStorageValue(item.actionid) == -1 then
       player:sendTextMessage(MESSAGE_EVENT_DEFAULT, "You have learned "..craftingProfessionsConfig[item.actionid].skillName..", then the book burned to ashes after learning it's secrets.")
       player:getPosition():sendMagicEffect(CONST_ME_EXPLOSIONHIT)
       player:setStorageValue(item.actionid, 10)
       Item(item.uid):remove(1)
     else
       player:sendCancelMessage("You already know "..craftingProfessionsConfig[item.actionid].skillName..".")
     end
   end
   return true
end
 
Everything looks fine at first glance.

Only thing to do at this point is start printing all the values, and make sure they are coming up as expected.
 
Back
Top