• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Lottery Action Script Issue

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,571
Solutions
3
Reaction score
98
Location
Portugal
hey there, I created this few months ago and now that I was reviewing it I had the idea to add amount of items given instead of give just 1

i wanted to give for example 100 cc instead of just 1 but the way I created it only allows to give 1

how can I make it work with like

Code:
local veryLucky = {{2160, 50}, {2160, 100}}


this is the script atm:

Code:
local unlucky = {2148, 2152}

local lucky = {2152, 2160}

local veryLucky = {2160, 2152}

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
    if not player:isPlayer() then
    return true
    end

    if player:getExhaustion(1000) <= 0 then
        player:setExhaustion(1000, 2)
    else
       player:sendTextMessage(MESSAGE_INFO_DESCR, "You're exhausted for: "..player:getExhaustion(1000).." seconds.")
    end

    if player:isPzLocked() then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You can't use this item while in combat.")
    return true
    end

    if player:getCondition(CONDITION_INFIGHT, CONDITIONID_DEFAULT) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You can't use this item while in combat.")
    return true
    end

    if player:getItemCount(item:getId()) <= 0 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Pickup this item before use it.")
    return true
    end

    if item:getActionId() == 2222 then
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "This item does not belong to you.")
    return true
    end
 

    local capacity = player:getCapacity()
    local chance = math.random(1,100)
    local broadcast = "Congratulations to "..player:getName().." for winning a "..ItemType(rewards3):getName().."."

    -- storages
    local unlucky_storage = STORAGE_UNLUCKY
    local lucky_storage = STORAGE_LUCKY
    local veryLucky_storage = STORAGE_VERY_LUCKY

    local storage = STORAGE_MAGIC_BOX_COUNTER
    local global_storage = GLOBALSTORAGE_MAGIC_BOX_COUNTER

    -- rewards
    local rewards3 = veryLucky[math.random(1, #veryLucky)]
    local rewards2 = lucky[math.random(1, #lucky)]
    local rewards1 = unlucky[math.random(1, #unlucky)]

    -- Lottery
    if chance == 100 and capacity > 200 then
        -- texts
        player:say(""..player:getName().." rolled a "..chance..".", TALKTYPE_MONSTER_SAY)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Congratulations you won "..ItemType(rewards3):getArticle().." "..ItemType(rewards3):getName().."!!!")
        Game.broadcastMessage(broadcast, MESSAGE_STATUS_WARNING)
        print(""..player:getName().." rolled "..chance.." and won "..ItemType(rewards3):getArticle().." "..ItemType(rewards3):getName()..".")
        --logs
        logItemUsage(player:getId(), item:getId(), 'data/logs/items/magic_box.log') -- TODO
        -- storages
        player:setStorageValue(veryLucky_storage, (player:getStorageValue(veryLucky_storage) or 0) + 1)
        player:setStorageValue(storage, (player:getStorageValue(storage) or 0) + 1)
        Game.setStorageValue(global_storage, (Game.getStorageValue(global_storage) or 0) + 1)
        --actions
        player:addItem(rewards3, 1)
        player:getPosition():sendMagicEffect(74)
        item:remove(1)
    elseif chance > 50 and chance < 100 and capacity > 200 then
        -- texts
        player:say(""..player:getName().." rolled a "..chance..".", TALKTYPE_MONSTER_SAY)
        player:sendTextMessage(MESSAGE_INFO_DESCR, ""..player:getName().." you won "..ItemType(rewards2):getArticle().." "..ItemType(rewards2):getName().."!")
        --logs
        logItemUsage(player:getId(), item:getId(), 'data/logs/items/magic_box.log') -- TODO
        -- storages
        player:setStorageValue(lucky_storage, (player:getStorageValue(lucky_storage) or 0) + 1)
        player:setStorageValue(storage, (player:getStorageValue(storage) or 0) + 1)
        Game.setStorageValue(global_storage, (Game.getStorageValue(global_storage) or 0) + 1)
        --actions
        player:addItem(rewards2, 1)
        player:getPosition():sendMagicEffect(75)
        item:remove(1)
     elseif chance >= 0 and chance < 50 and capacity > 200 then
        -- texts
        player:say(""..player:getName().." rolled a "..chance..".", TALKTYPE_MONSTER_SAY)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Sorry "..player:getName()..", better luck next time!\nYou won "..ItemType(rewards1):getArticle().." "..ItemType(rewards1):getName().."...")
        --logs
        logItemUsage(player:getId(), item:getId(), 'data/logs/items/magic_box.log') -- TODO
        -- storages
        player:setStorageValue(veryLucky_storage, (player:getStorageValue(unlucky_storage) or 0) + 1)
        player:setStorageValue(storage, (player:getStorageValue(storage) or 0) + 1)
        Game.setStorageValue(global_storage, (Game.getStorageValue(global_storage) or 0) + 1)
        --actions
        player:addItem(rewards1, 1)
        player:getPosition():sendMagicEffect(69)
        item:remove(1)
    else
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You failed opening the box, try again.")
        print(""..player:getName().." had problem with "..item:getName()..".")
    end
    return true
end

P.S: Sorry for the script being so big, I'm still trying to figure out a way to create outside functions to make it simple :p
 
Solution
Code:
local rewards = {
    {
        chanceMax = 49,
        logFile = 'data/logs/items/magic_box.log',
        items = {{2148, 1}, {2152, 1}},
        effect = 69,
        storage = STORAGE_UNLUCKY
    },
    {
        chanceMax = 99,
        logFile = 'data/logs/items/magic_box.log',
        items = {{2152, 1}, {2160, 1}},
        effect = 75,
        storage = STORAGE_LUCKY
    },
    {
        chanceMax = 100,
        logFile = 'data/logs/items/magic_box.log',
        items = {{2160, 1}, {2152, 1}},
        broadcast = true,
        effect = 74,
        storage = STORAGE_VERY_LUCKY
    },
}

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
    if player:getExhaustion(1000) > 0 then...
Code:
local rewards = {
    {
        chanceMax = 49,
        logFile = 'data/logs/items/magic_box.log',
        items = {{2148, 1}, {2152, 1}},
        effect = 69,
        storage = STORAGE_UNLUCKY
    },
    {
        chanceMax = 99,
        logFile = 'data/logs/items/magic_box.log',
        items = {{2152, 1}, {2160, 1}},
        effect = 75,
        storage = STORAGE_LUCKY
    },
    {
        chanceMax = 100,
        logFile = 'data/logs/items/magic_box.log',
        items = {{2160, 1}, {2152, 1}},
        broadcast = true,
        effect = 74,
        storage = STORAGE_VERY_LUCKY
    },
}

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
    if player:getExhaustion(1000) > 0 then
       player:sendTextMessage(MESSAGE_INFO_DESCR, "You're exhausted for: "..player:getExhaustion(1000).." seconds.")
       return true
    end

    player:setExhaustion(1000, 2)

    if player:getCondition(CONDITION_INFIGHT, CONDITIONID_DEFAULT) or player:isPzLocked() then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You can't use this item while in combat.")
        return true
    end

    if fromPosition.x ~= CONTAINER_POSITION then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Pickup this item before use it.")
        return true
    end

    if item:getActionId() == 2222 then
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "This item does not belong to you.")
        return true
    end


    local playerName = player:getName()
    if player:getCapacity() <= 200 then
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You failed opening the box, try again.")
        print(("%s had problem with %s."):format(playerName, item:getName()))
        return true
    end

    local chance = math.random(100)
    for i = 1, #rewards do
        local reward = rewards[i]
        if chance <= reward.maxChance then
            local rewardItem = reward.items[math.random(#reward.items)]
            local it = ItemType(rewardItem[1])
            player:say(("%s rolled a %i."):format(playerNamer, chance), TALKTYPE_MONSTER_SAY)

            if reward.broadcast then
                Game.broadcastMessage(("Congratulations to %s for winning a %s."):format(playerName, it:getName()), MESSAGE_STATUS_WARNING)
            end

            player:addItem(rewardItem[1], rewardItem[2] or 1)
            logItemUsage(player.uid, item.itemid, reward.logFile)
            player:getPosition():sendMagicEffect(reward.effect)
            player:setStorageValue(STORAGE_MAGIC_BOX_COUNTER, math.max(player:getStorageValue(STORAGE_MAGIC_BOX_COUNTER), 0) + 1)
            Game.setStorageValue(GLOBALSTORAGE_MAGIC_BOX_COUNTER, math.max(Game.getStorageValue(GLOBALSTORAGE_MAGIC_BOX_COUNTER) or 0, 0) + 1)
            player:setStorageValue(reward.storage, math.max(player:getStorageValue(reward.storage), 0) + 1)
            item:remove(1)

            if i == 1 then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "Sorry "..playerName..", better luck next time!\nYou won "..it:getArticle().." "..it:getName().."...")
            elseif i == 2 then
                player:sendTextMessage(MESSAGE_INFO_DESCR, playerName.." you won "..it:getArticle().." "..it:getName().."!")
            elseif i == 3 then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "Congratulations you won "..it:getArticle().." "..it:getName().."!!!")
            end
            return true
        end
    end

    return false
end
 
Solution
thank you so much, you are very kind and patient to rewrite the whole script :eek::p

I fixed some typos at line 61 and 64, nothing special just maxChance to chanceMax and playerNamer to playerName xd

1 more question could you please fix the part where if you get 100 crystal coins the script should say "You received "..amount.." crystal coins" instead of "You received a crystal coin"

here's the script without the typos:

Code:
local rewards = {
    {
        chanceMax = 49,
        logFile = 'data/logs/items/magic_box.log',
        items = {{2148, 1}, {2148, 12}},
        effect = 69,
        storage = STORAGE_UNLUCKY
    },
    {
        chanceMax = 99,
        logFile = 'data/logs/items/magic_box.log',
        items = {{2152, 1}, {2152, 12}},
        effect = 75,
        storage = STORAGE_LUCKY
    },
    {
        chanceMax = 100,
        logFile = 'data/logs/items/magic_box.log',
        items = {{2160, 1}, {2160, 12}},
        broadcast = true,
        effect = 74,
        storage = STORAGE_VERY_LUCKY
    },
}

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
    if player:getExhaustion(1000) > 0 then
       player:sendTextMessage(MESSAGE_INFO_DESCR, "You're exhausted for: "..player:getExhaustion(1000).." seconds.")
       return true
    end

    player:setExhaustion(1000, 2)

    if player:getCondition(CONDITION_INFIGHT, CONDITIONID_DEFAULT) or player:isPzLocked() then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You can't use this item while in combat.")
        return true
    end

    if fromPosition.x ~= CONTAINER_POSITION then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Pickup this item before use it.")
        return true
    end

    if item:getActionId() == 2222 then
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "This item does not belong to you.")
        return true
    end


    local playerName = player:getName()
    if player:getCapacity() <= 200 then
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You failed opening the box, try again.")
        print(("%s had problem with %s."):format(playerName, item:getName()))
        return true
    end

    local chance = math.random(100)
    for i = 1, #rewards do
        local reward = rewards[i]
        if chance <= reward.chanceMax then
            local rewardItem = reward.items[math.random(#reward.items)]
            local it = ItemType(rewardItem[1])
            player:say(("%s rolled a %i."):format(playerName, chance), TALKTYPE_MONSTER_SAY)

            if reward.broadcast then
                Game.broadcastMessage(("Congratulations to %s for winning a %s."):format(playerName, it:getName()), MESSAGE_STATUS_WARNING)
            end

            player:addItem(rewardItem[1], rewardItem[2] or 1)
            logItemUsage(player.uid, item.itemid, reward.logFile)
            player:getPosition():sendMagicEffect(reward.effect)
            player:setStorageValue(STORAGE_MAGIC_BOX_COUNTER, math.max(player:getStorageValue(STORAGE_MAGIC_BOX_COUNTER), 0) + 1)
            Game.setStorageValue(GLOBALSTORAGE_MAGIC_BOX_COUNTER, math.max(Game.getStorageValue(GLOBALSTORAGE_MAGIC_BOX_COUNTER) or 0, 0) + 1)
            player:setStorageValue(reward.storage, math.max(player:getStorageValue(reward.storage), 0) + 1)
            item:remove(1)

            if i == 1 then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "Sorry "..playerName..", better luck next time!\nYou won "..it:getArticle().." "..it:getName().."...")
            elseif i == 2 then
                player:sendTextMessage(MESSAGE_INFO_DESCR, playerName.." you won "..it:getArticle().." "..it:getName().."!")
            elseif i == 3 then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "Congratulations you won "..it:getArticle().." "..it:getName().."!!!")
            end
            return true
        end
    end

    return false
end
 
Code:
elseif i == 2 then
player:sendTextMessage(MESSAGE_INFO_DESCR, playerName.." you won "..it:getArticle().." "..it:getName().."!")
elseif i == 3 then
player:sendTextMessage(MESSAGE_INFO_DESCR, "Congratulations you won "..it:getArticle().." "..it:getName().."!!!")

To
Code:
  elseif i == 2 then
  player:sendTextMessage(MESSAGE_INFO_DESCR, playerName.." you won "..rewardItem[2].."x "..it:getName().."!")
  elseif i == 3 then
  player:sendTextMessage(MESSAGE_INFO_DESCR, "Congratulations you won "..rewardItem[2].."x "..it:getName().."!!!")
 
Back
Top