• 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 [1.2] Modal Window Help

strutZ

Australian OT Member {AKA Beastn}
Joined
Nov 16, 2014
Messages
1,393
Solutions
7
Reaction score
552
Hello Otland!

So I have been working on a modal window system that allows players to buy potions easier but i'm having some issues... If i buy 100 health potions then try and buy 100 of any other potion it will always give me health potions... Also is there a way to keep the window open until you press cancel? (i don't think so but maybe there is a trick i don't know)

ANYWAYS Here is the scripts. Thanks in advance.

action
Code:
local potions = {
    [1] = {potion = "Health Potion", itemID = 7618, price10 = 450, price50 = 2250, price100 = 4500},
    [2] = {potion = "Strong Health Potion", itemID = 7588, price10 = 1000, price50 = 5000, price100 = 10000},
    [3] = {potion = "Great Health Potion", itemID = 7591, price10 = 1900, price50 = 9500, price100 = 19000},
    [4] = {potion = "Ultimate Health Potion", itemID = 8473, price10 = 3100, price50 = 15500, price100 = 31000},
    [5] = {potion = "Supreme Health Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
    [6] = {potion = "Mana Potion", itemID = 7620, price10 = 500, price50 = 2500, price100 = 5000},
    [7] = {potion = "Strong Mana Potion", itemID = 7589, price10 = 800, price50 = 4000, price100 = 8000},
    [8] = {potion = "Great Mana Potion", itemID = 7590, price10 = 1200, price50 = 6000, price100 = 12000},
    [9] = {potion = "Ultimate Mana Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
    [10] = {potion = "Great Spirit Potion", itemID = 8472, price10 = 1900, price50 = 9500, price100 = 19000},
    [11] = {potion = "Ultimate Spirit Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
}

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

    local title = "Choose your Potion"
    local message = "Please select how many potions you would like to buy."

    local window = ModalWindow(1000, title, message)
    window:addButton(100, "Buy 50")
    window:addButton(101, "Buy 100")
    window:addButton(102, "Buy 10")
    window:addButton(103, "Cancel")
    window:setDefaultEnterButton(101)
    window:setDefaultEscapeButton(103)
  
    for i = 1, #potions do
    local o = potions[i].potion
        window:addChoice(i, o)
    end
   
    window:sendToPlayer(player)
    return true
end

Creature

Code:
local potions = {
    [1] = {potion = "Health Potion", itemID = 7618, price10 = 450, price50 = 2250, price100 = 4500},
    [2] = {potion = "Strong Health Potion", itemID = 7588, price10 = 1000, price50 = 5000, price100 = 10000},
    [3] = {potion = "Great Health Potion", itemID = 7591, price10 = 1900, price50 = 9500, price100 = 19000},
    [4] = {potion = "Ultimate Health Potion", itemID = 8473, price10 = 3100, price50 = 15500, price100 = 31000},
    [5] = {potion = "Supreme Health Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
    [6] = {potion = "Mana Potion", itemID = 7620, price10 = 500, price50 = 2500, price100 = 5000},
    [7] = {potion = "Strong Mana Potion", itemID = 7589, price10 = 800, price50 = 4000, price100 = 8000},
    [8] = {potion = "Great Mana Potion", itemID = 7590, price10 = 1200, price50 = 6000, price100 = 12000},
    [9] = {potion = "Ultimate Mana Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
    [10] = {potion = "Great Spirit Potion", itemID = 8472, price10 = 1900, price50 = 9500, price100 = 19000},
    [11] = {potion = "Ultimate Spirit Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
}
local moneyMsg = "You do not have enough money!" -- This is the message the player will recieve when he does not have enough money.
local capacityMsg = "I need more cap" -- This is the message the player will recieve when he does not have enough capactiy.
local buyMsg = "You have bought "
function onModalWindow(player, modalWindowId, buttonId, choiceId)
    player:unregisterEvent("mwBuyPotions")

    if modalWindowId == 1000 then
   
        -- Buy 50 Potions
        if buttonId == 100 then
            -- Check is player has enough money
            if player:getMoney() < (potions[choiceId].price50) then
                player:say(moneyMsg, TALKTYPE_MONSTER_SAY)
                return true
            end       
            -- Check if player has enough room/capacity to hold the items if true give items
            local potionEx = Game.createItem(potions[choiceId].itemID, 50)
            if player:addItemEx(potionEx) ~= RETURNVALUE_NOERROR then
                player:getPosition():sendMagicEffect(CONST_ME_POFF)
                player:say(capacityMsg, TALKTYPE_MONSTER_SAY)
                return true
            end
        -- Remove money from player and send buy confirmation
        player:removeMoney(potions[choiceId].price50)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, buyMsg.."50x "..potions[choiceId].potion.."s")
           
        -- Buy 100 Potions
        elseif buttonId == 101 then
            -- Check is player has enough money
            if player:getMoney() < (potions[choiceId].price100) then
                player:say(moneyMsg, TALKTYPE_MONSTER_SAY)
                return true
            end       
            -- Check if player has enough room/capacity to hold the items if true give items
            local potionEx = Game.createItem(potions[choiceId].itemID, 100)
            if player:addItemEx(potionEx) ~= RETURNVALUE_NOERROR then
                player:getPosition():sendMagicEffect(CONST_ME_POFF)
                player:say(capacityMsg, TALKTYPE_MONSTER_SAY)
                return true
            end
        -- Remove money from player and send buy confirmation
        player:removeMoney(potions[choiceId].price100)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, buyMsg.."100x "..potions[choiceId].potion.."s")

        -- Buy 10 Potions
        elseif buttonId == 102 then
            -- Check is player has enough money
            if player:getMoney() < (potions[choiceId].price10) then
                player:say(moneyMsg, TALKTYPE_MONSTER_SAY)
                return true
            end       
            -- Check if player has enough room/capacity to hold the items if true give items
            local potionEx = Game.createItem(potions[choiceId].itemID, 10)
            if player:addItemEx(potionEx) ~= RETURNVALUE_NOERROR then
                player:getPosition():sendMagicEffect(CONST_ME_POFF)
                player:say(capacityMsg, TALKTYPE_MONSTER_SAY)
                return true
            end
        -- Remove money from player and send buy confirmation
        player:removeMoney(potions[choiceId].price10)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, buyMsg.."10x "..potions[choiceId].potion.."s")
        end
    end
end
 
TBH not really too sure how to use that.
Code:
function Player:sendShopWindow(potions)
    local function buttonCallback(button, choice)
        local moneyMsg = "You do not have enough money!" -- This is the message the player will recieve when he does not have enough money.
        local capacityMsg = "I need more cap" -- This is the message the player will recieve when he does not have enough capactiy.
        local buyMsg = "You have bought "
     
        local count = 0
        local price = 0
        local potion = potions[choice.id]
     
        if not potion then
            print("Something went wrong with choice id: " .. choice.id)
            return false
        end
     
        if button.text == "Buy 100" then
            count = 100
            price = potion.price100
        elseif button.text == "Buy 50" then
            count = 50
            price = potion.price50
        elseif button.text == "Buy 10" then
            count = 10
            price = potion.price10
        else
            print("Something went wrong with button: " .. button.text)
            return false
        end
     
        if self:getMoney() < price then
            self:say(moneyMsg, TALKTYPE_MONSTER_SAY)
            return false
        end
     
        local potionEx = Game.createItem(potion.itemID, count)
        if self:addItemEx(potionEx, false) ~= RETURNVALUE_NOERROR then
            self:getPosition():sendMagicEffect(CONST_ME_POFF)
            self:say(capacityMsg, TALKTYPE_MONSTER_SAY)
            return true
        end
     
        self:removeMoney(price)
        self:sendTextMessage(MESSAGE_EVENT_ADVANCE, buyMsg .. count .. "x ".. potion.potion .."s")
        --self:sendShopWindow(potions) -- If you want to send the window again until he press cancel.
    end
 
    local window = ModalWindow {
        title = "Choose your Potion",
        message = "Please select how many potions you would like to buy.",
    }
    window:addButton("Buy 50", buttonCallback)
    window:addButton("Buy 100", buttonCallback)
    window:addButton("Buy 10", buttonCallback)
    window:addButton("Cancel")
    window:setDefaultEnterButton("Buy 100")
    window:setDefaultEscapeButton("Cancel")
   
    for i = 1, #potions do
        local o = potions[i].potion
        local choice = window:addChoice(o)
        choice.id = i
    end
    
    window:sendToPlayer(self)
end

Did not test tho.
Use like:
Code:
local potions = {
    [1] = {potion = "Health Potion", itemID = 7618, price10 = 450, price50 = 2250, price100 = 4500},
    [2] = {potion = "Strong Health Potion", itemID = 7588, price10 = 1000, price50 = 5000, price100 = 10000},
    [3] = {potion = "Great Health Potion", itemID = 7591, price10 = 1900, price50 = 9500, price100 = 19000},
    [4] = {potion = "Ultimate Health Potion", itemID = 8473, price10 = 3100, price50 = 15500, price100 = 31000},
    [5] = {potion = "Supreme Health Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
    [6] = {potion = "Mana Potion", itemID = 7620, price10 = 500, price50 = 2500, price100 = 5000},
    [7] = {potion = "Strong Mana Potion", itemID = 7589, price10 = 800, price50 = 4000, price100 = 8000},
    [8] = {potion = "Great Mana Potion", itemID = 7590, price10 = 1200, price50 = 6000, price100 = 12000},
    [9] = {potion = "Ultimate Mana Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
    [10] = {potion = "Great Spirit Potion", itemID = 8472, price10 = 1900, price50 = 9500, price100 = 19000},
    [11] = {potion = "Ultimate Spirit Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
}

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
    player:sendShopWindow(potions)
    return true
end

If you have any doubts you can look at this code as well for reference:
http://pastebin.com/vP76PWdb

Make sure you have the lib properly installed, you don't have to register anything anywhere just create the creaturescript with the tag specified in the topic and the lib.

Going to sleep if it doesn't work i'll helpya tomorrow
 
Code:
function Player:sendShopWindow(potions)
    local function buttonCallback(button, choice)
        local moneyMsg = "You do not have enough money!" -- This is the message the player will recieve when he does not have enough money.
        local capacityMsg = "I need more cap" -- This is the message the player will recieve when he does not have enough capactiy.
        local buyMsg = "You have bought "
    
        local count = 0
        local price = 0
        local potion = potions[choice.id]
    
        if not potion then
            print("Something went wrong with choice id: " .. choice.id)
            return false
        end
    
        if button.text == "Buy 100" then
            count = 100
            price = potion.price100
        elseif button.text == "Buy 50" then
            count = 50
            price = potion.price50
        elseif button.text == "Buy 10" then
            count = 10
            price = potion.price10
        else
            print("Something went wrong with button: " .. button.text)
            return false
        end
    
        if self:getMoney() < price then
            self:say(moneyMsg, TALKTYPE_MONSTER_SAY)
            return false
        end
    
        local potionEx = Game.createItem(potion.itemID, count)
        if self:addItemEx(potionEx, false) ~= RETURNVALUE_NOERROR then
            self:getPosition():sendMagicEffect(CONST_ME_POFF)
            self:say(capacityMsg, TALKTYPE_MONSTER_SAY)
            return true
        end
    
        self:removeMoney(price)
        self:sendTextMessage(MESSAGE_EVENT_ADVANCE, buyMsg .. count .. "x ".. potion.potion .."s")
        --self:sendShopWindow(potions) -- If you want to send the window again until he press cancel.
    end

    local window = ModalWindow {
        title = "Choose your Potion",
        message = "Please select how many potions you would like to buy.",
    }
    window:addButton("Buy 50", buttonCallback)
    window:addButton("Buy 100", buttonCallback)
    window:addButton("Buy 10", buttonCallback)
    window:addButton("Cancel")
    window:setDefaultEnterButton("Buy 100")
    window:setDefaultEscapeButton("Cancel")
  
    for i = 1, #potions do
        local o = potions[i].potion
        local choice = window:addChoice(o)
        choice.id = i
    end
   
    window:sendToPlayer(self)
end

Did not test tho.
Use like:
Code:
local potions = {
    [1] = {potion = "Health Potion", itemID = 7618, price10 = 450, price50 = 2250, price100 = 4500},
    [2] = {potion = "Strong Health Potion", itemID = 7588, price10 = 1000, price50 = 5000, price100 = 10000},
    [3] = {potion = "Great Health Potion", itemID = 7591, price10 = 1900, price50 = 9500, price100 = 19000},
    [4] = {potion = "Ultimate Health Potion", itemID = 8473, price10 = 3100, price50 = 15500, price100 = 31000},
    [5] = {potion = "Supreme Health Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
    [6] = {potion = "Mana Potion", itemID = 7620, price10 = 500, price50 = 2500, price100 = 5000},
    [7] = {potion = "Strong Mana Potion", itemID = 7589, price10 = 800, price50 = 4000, price100 = 8000},
    [8] = {potion = "Great Mana Potion", itemID = 7590, price10 = 1200, price50 = 6000, price100 = 12000},
    [9] = {potion = "Ultimate Mana Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
    [10] = {potion = "Great Spirit Potion", itemID = 8472, price10 = 1900, price50 = 9500, price100 = 19000},
    [11] = {potion = "Ultimate Spirit Potion", itemID = 7618, price10 = 760, price50 = 760, price100 = 760},
}

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
    player:sendShopWindow(potions)
    return true
end

If you have any doubts you can look at this code as well for reference:
http://pastebin.com/vP76PWdb

Make sure you have the lib properly installed, you don't have to register anything anywhere just create the creaturescript with the tag specified in the topic and the lib.

Going to sleep if it doesn't work i'll helpya tomorrow

You legend man thanks for the help!
 
Back
Top