Can anyone help me with this script?
I have that error whenever I try to use a lever
sacrifice.lua
actions.xml
[TFS 1.3] Script to sacrifice 3 of the same item and get a new one
Hey! I was messing up a 10.98 RL map with remere's map editor when I found these. After some time and some nasty edits I came up with some cool weapon concepts but I'm missing something: the possibility to upgrade from tier to tier. My idea is to have a script to combine three of these awesome...
otland.net
Lua:
Lua Script Error: [Action Interface]
data/actions/scripts/custom/sacrifice.lua:onUse
data/actions/scripts/custom/sacrifice.lua:54: attempt to index a nil value
stack traceback:
[C]: in function '__index'
data/actions/scripts/custom/sacrifice.lua:54: in function <data/actions/scripts/custom/sacrifice.lua:19>
I have that error whenever I try to use a lever
sacrifice.lua
Code:
--table structure:
--[ItemToUpgradeId] = {result = NewItemId, chance = chance to sucess}
local itensUpgrade = {
[25919] = {result = 25920, chance = 60},
[25920] = {result = 25921, chance = 40},
[25921] = {result = 25922, chance = 20}
}
--put a index and a tile for position, you can add more, just be sure to config.
--NOTE: this script is for a single altar, don't work with more than one at same time. Need changes or multiples files to more altars.
local sacrificePos = {
[1] = Tile(113, 136, 7),
[2] = Tile(114, 137, 7),
[3] = Tile(115, 136, 7)
}
local newItemPos = Tile(115, 137, 7)
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
local targetItem = -1 --initialize a variable to hold target item id
local itensToRemove = {} --initialize a table to hold all itens userData to remove
for _, tempPos in ipairs(sacrificePos) do --for each sacrifice position
local topItem = tempPos:getTopDownItem() --get top item of the position
--Top item don't exist
if not topItem then
return true
end
--Top item in some sacrifice position don't is a upgradable item
if not itensUpgrade[topItem:getId()] then
return true
end
--for first loop occurrence, set valid item to upgrade as target item
if targetItem == -1 then
targetItem = topItem:getId()
end
--some sacrifice item is diferent of other
if targetItem ~= topItem:getId() then
return true
end
table.insert(itensToRemove, topItem)--add the item userData to table
end
local randChance = math.random(1, 100)--get a random number
local effect = 12--initialize a variable to hold effect to be sended to player
local msg = ""--initialize a variable to hold msg to be sended to player
local materialCountToRemove = 0--initialize a variable to hold the count of itens that will be removed
--if the random chance number is lower or equal to chance to sucess
if randChance <= itensUpgrade[targetItem].chance then
--sucess
materialCountToRemove = #itensToRemove --set remove count to ALL itens in table
msg = "Congratulations, your item has been upgrated to "..ItemType(itensUpgrade[targetItem].result):getName().."." --update msg
effect = CONST_ME_FIREWORK_RED --update effect
Game.createItem(itensUpgrade[targetItem].result, 1, newItemPos:getPosition()) --create the new item
else
--fail
materialCountToRemove = math.random(1, #itensToRemove)--get a random chance to remove from 1 to ALL itens
msg = "You failed to upgrade your item. You've lost "..materialCountToRemove.." "..ItemType(targetItem):getName().."."--update msg
effect = CONST_ME_POFF--update effect
end
--send effect and msg
player:getPosition():sendMagicEffect(effect)
player:sendTextMessage(MESSAGE_STATUS_SMALL, msg)
--remove material itens
for i = 1, materialCountToRemove do
--don't know if needed, don't know if when removing the item, it will be removed from table too?
local tmpItem = itensToRemove[i]
itensToRemove[i] = 0 --set to 0 the value of i Index, to preserv table size if removed itens is also removed from table
--itensToRemove[i]:remove()
tmpItem:remove()
end
return true
end
actions.xml
Code:
<action uniqueid="11206" script="custom/sacrifice.lua"/>
<action actionid="11206" script="custom/sacrifice.lua"/>