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

Error Addon system

Wanheda

New Member
Joined
Feb 17, 2016
Messages
44
Solutions
2
Reaction score
4
Hello guys! I have a problem in a script addon system using onModalWindow .

The script works without any error , but when the character is already with all addons it appears : You have all the outfits ! You have been Awarded the achievement and a custom outfit!
and a button to confirm and cancel another when grip on confirm was close , but it is giving error and removing the item.
Just like that does not remove the item when grip on confirm that message.

Error:
Code:
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/modalAD.lua:onModalWindow
data/creaturescripts/scripts/modalAD.lua:61: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/creaturescripts/scripts/modalAD.lua:61: in function <data/creaturescripts/scripts/modalAD.lua:47>

creaturescripts\scripts\modalAD
Code:
local outfits = {
        [1] = {name = "Citizen", male = 128, female = 136},
        [2] = {name = "Hunter", male = 129, female = 137},
        [3] = {name = "Mage", male = 130, female = 138},
        [4] = {name = "Knight", male = 131, female = 139},
        [5] = {name = "Noble", male = 132, female = 140},
        [6] = {name = "Summoner", male = 133, female = 141},
        [7] = {name = "Warrior", male = 134, female = 142},
        [8] = {name = "Barbarian", male = 143, female = 147},
        [9] = {name = "Druid", male = 144, female = 148},
        [10] = {name = "Wizard", male = 145, female = 149},
        [11] = {name = "Oriental", male = 146, female = 150},
        [12] = {name = "Pirate", male = 151, female = 155},
        [13] = {name = "Assassin", male = 152, female = 156},
        [14] = {name = "Beggar", male = 153, female = 157},
        [15] = {name = "Shaman", male = 154, female = 158},
        [16] = {name = "Norse", male = 251, female = 252},
        [17] = {name = "Nightmare", male = 268, female = 269},
        [18] = {name = "Jester", male = 273, female = 270},
        [19] = {name = "Brotherhood", male = 278, female = 279},
        [20] = {name = "Demonhunter", male = 289, female = 288},
        [21] = {name = "Yalaharian", male = 325, female = 324},
        [22] = {name = "Warmaster", male = 335, female = 336},
        [23] = {name = "Wayfarer", male = 367, female = 366},
        [24] = {name = "Afflicted", male = 430, female = 431},
        [25] = {name = "Elementalist", male = 432, female = 433},
        [26] = {name = "Deepling", male = 463, female = 464},
        [27] = {name = "Insectoid", male = 465, female = 466},
        [28] = {name = "Entrepreneur", male = 472, female = 471},
        [29] = {name = "Crystal Warlord", male = 512, female = 513},
        [30] = {name = "Soil Guardian", male = 516, female = 514},
        [31] = {name = "Demon", male = 541, female = 542},
        [32] = {name = "Cave Explorer", male = 574, female = 575},
        [33] = {name = "Dream Warden", male = 577, female = 578},
        [34] = {name = "Champion", male = 633, female = 632},
        [35] = {name = "Conjurer", male = 634, female = 635},
        [36] = {name = "Beastmaster", male = 637, female = 636},
        [37] = {name = "Chaos Acolyte", male = 665, female = 664},
        [38] = {name = "Death Herald", male = 667, female = 666},
        [39] = {name = "Ranger", male = 684, female = 683},
        [40] = {name = "Ceremonial Garb", male = 695, female = 694},
        [41] = {name = "Puppeteer", male = 697, female = 696},
        [42] = {name = "Spirit Caller", male = 699, female = 698},
        [43] = {name = "Glooth Engineer", male = 610, female = 618},
    }
   
function onModalWindow(player, modalWindowId, buttonId, choiceId)
    player:unregisterEvent("modalAD")
   
    if modalWindowId == 1000 then
        if buttonId == 100 then
            if player:getItemCount(11401) == 0 then
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You must have a Addon Doll in your backpack!")
                return false
            end
            if choiceId == 0 then
                return false
            end
            player:removeItem(11401, 1)
            if player:getSex() == 0 then
                    player:addOutfitAddon(outfits[choiceId].female, 3)
                else
                    player:addOutfitAddon(outfits[choiceId].male, 3)
            end
            player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
        end
    end
end
actions\scripts\modalAD
Code:
local outfits = {
        [1] = {name = "Citizen", male = 128, female = 136},
        [2] = {name = "Hunter", male = 129, female = 137},
        [3] = {name = "Mage", male = 130, female = 138},
        [4] = {name = "Knight", male = 131, female = 139},
        [5] = {name = "Noble", male = 132, female = 140},
        [6] = {name = "Summoner", male = 133, female = 141},
        [7] = {name = "Warrior", male = 134, female = 142},
        [8] = {name = "Barbarian", male = 143, female = 147},
        [9] = {name = "Druid", male = 144, female = 148},
        [10] = {name = "Wizard", male = 145, female = 149},
        [11] = {name = "Oriental", male = 146, female = 150},
        [12] = {name = "Pirate", male = 151, female = 155},
        [13] = {name = "Assassin", male = 152, female = 156},
        [14] = {name = "Beggar", male = 153, female = 157},
        [15] = {name = "Shaman", male = 154, female = 158},
        [16] = {name = "Norse", male = 251, female = 252},
        [17] = {name = "Nightmare", male = 268, female = 269},
        [18] = {name = "Jester", male = 273, female = 270},
        [19] = {name = "Brotherhood", male = 278, female = 279},
        [20] = {name = "Demonhunter", male = 289, female = 288},
        [21] = {name = "Yalaharian", male = 325, female = 324},
        [22] = {name = "Warmaster", male = 335, female = 336},
        [23] = {name = "Wayfarer", male = 367, female = 366},
        [24] = {name = "Afflicted", male = 430, female = 431},
        [25] = {name = "Elementalist", male = 432, female = 433},
        [26] = {name = "Deepling", male = 463, female = 464},
        [27] = {name = "Insectoid", male = 465, female = 466},
        [28] = {name = "Entrepreneur", male = 472, female = 471},
        [29] = {name = "Crystal Warlord", male = 512, female = 513},
        [30] = {name = "Soil Guardian", male = 516, female = 514},
        [31] = {name = "Demon", male = 541, female = 542},
        [32] = {name = "Cave Explorer", male = 574, female = 575},
        [33] = {name = "Dream Warden", male = 577, female = 578},
        [34] = {name = "Champion", male = 633, female = 632},
        [35] = {name = "Conjurer", male = 634, female = 635},
        [36] = {name = "Beastmaster", male = 637, female = 636},
        [37] = {name = "Chaos Acolyte", male = 665, female = 664},
        [38] = {name = "Death Herald", male = 667, female = 666},
        [39] = {name = "Ranger", male = 684, female = 683},
        [40] = {name = "Ceremonial Garb", male = 695, female = 694},
        [41] = {name = "Puppeteer", male = 697, female = 696},
        [42] = {name = "Spirit Caller", male = 699, female = 698},
    }

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
    player:registerEvent("modalAD")

    local title = "Choose your outfit!"
    local message = "You will receive the outfit and both addons!"

    local window = ModalWindow(1000, title, message)
    if player:getItemCount(11401) >= 1 then
        window:addButton(100, "Confirm")
        window:setDefaultEnterButton(100)
    else
        window:setDefaultEnterButton(101)
end
    window:addButton(101, "Cancel")
    window:setDefaultEscapeButton(101)
  
    for i = 1, #outfits do
        local o = outfits[i].name
        if not player:hasOutfit(outfits[i].male, 3) and not player:hasOutfit(outfits[i].female, 3) then
            if outfits[i].name == "Noble" or outfits[i].name == "Norse" then
                if player:getSex() == 0 then
                    o = o .. "woman"
                else
                    o = o .. "man"
                end
            end
            window:addChoice(i, o)
        end
    end
   
    if window:getChoiceCount() == 0 then
        window:setMessage("You have all the outfits! You have been awarded the achievement and a custom outfit!")
        --add achievement
    end

    window:sendToPlayer(player)
    return true
end
 
The error is somewhat straight forward to read.
I'll give you and idea on how to trouble shoot this since I don't want to create a discussion about this in the thread, although that just might happen anyway.

We will use colors to show how to read the error then a step by step break down on how to narrow down where the error might be taking place.
Path & file where the error appears
Line number of the possible error
The error of the problem which the server thinks is on that line
The line number and file of where the error is stemming from

data/creaturescripts/scripts/modalAD.lua:61: attempt to index a nil value

in function <data/creaturescripts/scripts/modalAD.lua:47>

How to resolve? Well if we look at line 61,
Code:
player:addOutfitAddon(outfits[choiceId].female, 3)
Well we know player exist otherwise we would not make it this far in the script,

We know that addOutfitAddon is a valid tfs metamethod acessible by player
We know that outfits is a table and that female is 1 of its index's property, we also know the second argument of addOutfitAddon, 3 gives all the addons.

The one thing we don't know is what the value of choiceId is,
I would comment out this line, and then use a print function on choiceId to see what value is being passed.
Like this
Code:
--player:addOutfitAddon(outfits[choiceId].female, 3)
print(choiceId)

And then replicate the action which caused the error, once you know what value is being passed, it will be easier to find a solution to the problem, this method of trouble shooting can be applied to any script, it is not just limited to this one.

It is overall called the process of elimination.
 
The error is somewhat straight forward to read.
I'll give you and idea on how to trouble shoot this since I don't want to create a discussion about this in the thread, although that just might happen anyway.

We will use colors to show how to read the error then a step by step break down on how to narrow down where the error might be taking place.
Path & file where the error appears
Line number of the possible error
The error of the problem which the server thinks is on that line
The line number and file of where the error is stemming from

data/creaturescripts/scripts/modalAD.lua:61: attempt to index a nil value

in function <data/creaturescripts/scripts/modalAD.lua:47>

How to resolve? Well if we look at line 61,
Code:
player:addOutfitAddon(outfits[choiceId].female, 3)
Well we know player exist otherwise we would not make it this far in the script,

We know that addOutfitAddon is a valid tfs metamethod acessible by player
We know that outfits is a table and that female is 1 of its index's property, we also know the second argument of addOutfitAddon, 3 gives all the addons.

The one thing we don't know is what the value of choiceId is,
I would comment out this line, and then use a print function on choiceId to see what value is being passed.
Like this
Code:
--player:addOutfitAddon(outfits[choiceId].female, 3)
print(choiceId)

And then replicate the action which caused the error, once you know what value is being passed, it will be easier to find a solution to the problem, this method of trouble shooting can be applied to any script, it is not just limited to this one.

It is overall called the process of elimination.
It's pretty nice of you to help people out with their bugged scripts and not only post the working one but help them solve their future errors by themselves.
Hats off to you.

EDIT: I've stopped scripting since protocol version 8.6. And never been good at it actually, used to do weak scripts like custom manarunes, sds, etc. by replacing a couple lines.
So I don't understand the print line and what its function is, I know you've done your best to explain it but my English isn't helping me at all. Please explain further.
 
So I don't understand the print line and what its function is, I know you've done your best to explain it but my English isn't helping me at all. Please explain further.
Print, just takes any value and prints it to the console window even if the value is nil.
Code:
local x = 4
print(x)
-- output 
4
print(x * 2)
-- output
8
Its just a mechanism we use to troubleshoot a script when the script isn't working as intended.

However there are other methods of printing values such as io.write(), the difference between the 2 is if you run an iterator (loop) and print values using print then the values appear on their own line, where as io.write() they appear on the same line.
Code:
local x = 5
print(x)
io.write(x)
-- output for both
5
 
Print, just takes any value and prints it to the console window even if the value is nil.
Code:
local x = 4
print(x)
-- output
4
print(x * 2)
-- output
8
Its just a mechanism we use to troubleshoot a script when the script isn't working as intended.

However there are other methods of printing values such as io.write(), the difference between the 2 is if you run an iterator (loop) and print values using print then the values appear on their own line, where as io.write() they appear on the same line.
Code:
local x = 5
print(x)
io.write(x)
-- output for both
5
What if choiceID has no value? It'll output 0?
 
I did not understand very well, but understand what the @Codex NG explained .
It would be easier to take this window which appears saying that the player already has all the addon , and put something in the local chat . Something like " Have all addon " .
I do not have much knowledge in .lua so it is difficult to understand , but support his idea tries to explain how to solve the problem.
 
Code:
if outfits[choiceId] then
    player:addOutfitAddon(outfits[choiceId].female, 3)
end

that's what I meant in addition to what ~Codex said.
 

Similar threads

Back
Top