• 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 Modal Window TFS 1.2 10.98

Itutorial

Legendary OT User
Joined
Dec 23, 2014
Messages
2,332
Solutions
68
Reaction score
1,009
I have never run into this problem before. Here is whats going on.


The codes:

Lua:
local stats = {
    [1] = {id = 0x01, name = 'Health'},
    [2] = {id = 0x02, name = 'Health Regen'},
    [3] = {id = 0x03, name = 'Mana'},
    [4] = {id = 0x04, name = 'Mana Regen'},
    [5] = {id = 0x05, name = 'Physical Damage'},
    [6] = {id = 0x06, name = 'Magic Damage'},
    [7] = {id = 0x07, name = 'Healing'},
    [8] = {id = 0x08, name = 'Physical Defense'},
    [9] = {id = 0x09, name = 'Magic Defense'}
}

function onSay(player, words, param)
    window = ModalWindow(6, 'Stat Point Shop', 'You have '..player:getStatPoints()..' stat points to spend')
   
    for i = 1, #stats do
        window:addChoice(stats[i].id, stats[i].name)
    end

    window:addButton(1, 'Buy')
    window:setDefaultEnterButton(1)
    window:addButton(2, 'Close')
    window:setDefaultEscapeButton(2)
    window:sendToPlayer(player)
    return false
end

Lua:
function onModalWindow(player, modalWindowId, choiceId, buttonId)
    if modalWindowId ~= 6 then return true end
        if buttonId ~= 1 then return true end
            if player:getStatPoints() < 1 then
                return player:sendCancelMessage("You do not have any stat points to spend.")
            end
       
            if choiceId == 0x01 then
                player:addExtraHealth(1)
                player:setMaxHealth(player:getMaxHealth() + 5)
                player:addStatPoints(-1)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your health by 5. One stat point has been used.")
            end
            if choiceId == 0x02 then
                player:addHealthRegen(1)
                player:addStatPoints(-1)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your health regeneration by 2. One stat point has been used.")
            end
            if choiceId == 0x03 then
                player:addExtraMana(1)
                player:setMaxMana(player:getMaxMana() + 5)
                player:addStatPoints(-1)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your mana by 5. One stat point has been used.")
            end
            if choiceId == 0x04 then
                player:addManaRegen(1)
                player:addStatPoints(-1)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your mana regeneration by 2. One stat point has been used.")
            end
            if choiceId == 0x05 then
                player:addExtraDamage(1)
                player:addStatPoints(-1)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your physical damage. One stat point has been used.")
            end
            if choiceId == 0x06 then
                player:addExtraMagicDamage(1)
                player:addStatPoints(-1)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your magic damage. One stat point has been used.")
            end
            if choiceId == 0x07 then
                player:addExtraHealing(1)
                player:addStatPoints(-1)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your healing. One stat point has been used.")
            end
            if choiceId == 0x08 then
                player:addPhysicalDefense(1)
                player:addStatPoints(-1)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your physical defense. One stat point has been used.")
            end
            if choiceId == 0x09 then
                player:addMagicDefense(1)
                player:addStatPoints(-1)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your magic defense. One stat point has been used.")
            end
return true
end
 
Solution
just follow the tutorial on how to install it, you create a new lib file (or put it inside an existing one) and register an event for onModalWindow in creaturescripts with the code he provided
he also gave an example of how to use it, but i'll rewrite it for you to show you how it would work with the helper
Lua:
local stats = {
    [1] = {name = 'Health'},
    [2] = {name = 'Health Regen'},
    [3] = {name = 'Mana'},
    [4] = {name = 'Mana Regen'},
    [5] = {name = 'Physical Damage'},
    [6] = {name = 'Magic Damage'},
    [7] = {name = 'Healing'},
    [8] = {name = 'Physical Defense'},
    [9] = {name = 'Magic Defense'}
}

function onSay(player, words, param)
    local points = player:getStatPoints()
    if points < 1 then...
highly recommend you use this
[TFS 1.2] Modal Window Helper Lib
it'll make it so much easier to use modal windows, you can set callbacks for your buttons instead of having to script like you're doing now

but, if you don't want to use it
try to print out buttonId and make sure you're checking for the right one (i see you put 1 for the id for buy and you're returning true for ~= 1 but print out the id anyways)
 
It prints 1 but only when I click on the first option and press buy, or if I click cancel.

Im really not sure how to use the helper.
 
just follow the tutorial on how to install it, you create a new lib file (or put it inside an existing one) and register an event for onModalWindow in creaturescripts with the code he provided
he also gave an example of how to use it, but i'll rewrite it for you to show you how it would work with the helper
Lua:
local stats = {
    [1] = {name = 'Health'},
    [2] = {name = 'Health Regen'},
    [3] = {name = 'Mana'},
    [4] = {name = 'Mana Regen'},
    [5] = {name = 'Physical Damage'},
    [6] = {name = 'Magic Damage'},
    [7] = {name = 'Healing'},
    [8] = {name = 'Physical Defense'},
    [9] = {name = 'Magic Defense'}
}

function onSay(player, words, param)
    local points = player:getStatPoints()
    if points < 1 then
        return player:sendCancelMessage("You do not have any stat points to spend") and false
    end

    local window = ModalWindow { title = 'Stat Point Shop', message = 'You have '.. points ..' stat points to spend' }
   
    for i = 1, #stats do
        window:addChoice(stats[i].name)
    end

    window:addButton('Buy', ( 
            function(button, choice)
              if choice.id == 1 then
                   player:addHealthRegen(1)
                   player:addStatPoints(-1)
                   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your health regeneration by 2. One stat point has been used.")
               end
               if choice.id == 2 then
                   player:addExtraMana(1)
                   player:setMaxMana(player:getMaxMana() + 5)
                   player:addStatPoints(-1)
                   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your mana by 5. One stat point has been used.")
               end
               if choice.id == 3 then
                   player:addManaRegen(1)
                   player:addStatPoints(-1)
                   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your mana regeneration by 2. One stat point has been used.")
               end
               if choice.id == 4 then
                   player:addExtraDamage(1)
                   player:addStatPoints(-1)
                   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your physical damage. One stat point has been used.")
               end
               if choice.id == 5 then
                   player:addExtraMagicDamage(1)
                   player:addStatPoints(-1)
                   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your magic damage. One stat point has been used.")
               end
               if choice.id == 6 then
                   player:addExtraHealing(1)
                   player:addStatPoints(-1)
                   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your healing. One stat point has been used.")
               end
               if choice.id == 7 then
                   player:addPhysicalDefense(1)
                   player:addStatPoints(-1)
                   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your physical defense. One stat point has been used.")
               end
               if choice.id == 8 then
                   player:addMagicDefense(1)
                   player:addStatPoints(-1)
                   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have increased your magic defense. One stat point has been used.")
               end
            end
        )
    )
    window:setDefaultEnterButton('Buy')
    window:addButton('Close')
    window:setDefaultEscapeButton('Close')
    window:sendToPlayer(player)
    return false
end
 
Solution
Back
Top