• 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 Help optimizing script

  • Thread starter Thread starter Evil Puncker
  • Start date Start date
E

Evil Puncker

Guest
I found an old script here in otland for large seashell and wanted to update it in order to PR into tfs, help with optimization, logic and with chances (yeah i'm kinda dumb)


  • item can only be opened each 20 hours
  • the text is displayed on the item position not on player position
  • the pearl is created on player position
  • both open and closed seashells should display the "You have already opened a shell today." after opened
  • chances should be: (48% pearl)(16% finger squeezed)(36% nothing)

LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local timenow = os.time()
    local nextday = timenow + 72000
    local CHANCES = math.random(100)
    if item.itemid == 7552 then
        if player:getStorageValue(75520) < timenow then
                if CHANCES <= 20 then
                    Game.createItem(math.random(7632,7633), 1, player:getPosition())
                    toPosition:say("You found a beautiful pearl",TALKTYPE_MONSTER_SAY)
                elseif (CHANCES <= 60 and CHANCES > 20) then
                    player:addHealth(-200)
                    toPosition:say("Ouch! You squeezed your fingers",TALKTYPE_MONSTER_SAY)
                else
                    toPosition:say("Nothing is inside",TALKTYPE_MONSTER_SAY)
                end
                player:setStorageValue(75520, nextday)
                item:transform(item.itemid+1)
                item:decay()
            else
                toPosition:say("You have already opened a shell today.",TALKTYPE_MONSTER_SAY)
        end
    elseif item.itemid == 7553 then
        toPosition:say("You have already opened a shell today.",TALKTYPE_MONSTER_SAY)
    end
    return true
end
 
Solution
I've just found out that the open seashell has no message whatsoever, only the closed one, and that when you find a pear it displays some sort of bubbles effect:

ExDQyLx.gif
--Added bubble effects
--Removed msg that already opened a shell when its already open

Anything missing?
--Edited only the script that don't show the text to all spectators. Tell me which one are you using...
LUA:
--configuration table
local config = {
    cooldownTime = 72000, --time in seconds that player will be Exhausted
    cooldownStorage = 43000, --storage to hold the cooldown of Exhaustion
    pearlRewards = {7632, 7633}, --table with itemIds of the possible rewards
    closedShellItemId = 7552, --closed shell itemId...
I found an old script here in otland for large seashell and wanted to update it in order to PR into tfs, help with optimization, logic and with chances (yeah i'm kinda dumb)


  • item can only be opened each 20 hours
  • the text is displayed on the item position not on player position
  • the pearl is created on player position
  • both open and closed seashells should display the "You have already opened a shell today." after opened
  • chances should be: (48% pearl)(16% finger squeezed)(36% nothing)

LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local timenow = os.time()
    local nextday = timenow + 72000
    local CHANCES = math.random(100)
    if item.itemid == 7552 then
        if player:getStorageValue(75520) < timenow then
                if CHANCES <= 20 then
                    Game.createItem(math.random(7632,7633), 1, player:getPosition())
                    toPosition:say("You found a beautiful pearl",TALKTYPE_MONSTER_SAY)
                elseif (CHANCES <= 60 and CHANCES > 20) then
                    player:addHealth(-200)
                    toPosition:say("Ouch! You squeezed your fingers",TALKTYPE_MONSTER_SAY)
                else
                    toPosition:say("Nothing is inside",TALKTYPE_MONSTER_SAY)
                end
                player:setStorageValue(75520, nextday)
                item:transform(item.itemid+1)
                item:decay()
            else
                toPosition:say("You have already opened a shell today.",TALKTYPE_MONSTER_SAY)
        end
    elseif item.itemid == 7553 then
        toPosition:say("You have already opened a shell today.",TALKTYPE_MONSTER_SAY)
    end
    return true
end
You mean update to revscripts for 1.3?
I can adjust for what you want, but will be in 1.2. Let me know, when i get home i can do...
 
To do this:
"the text is displayed on the item position not on player position"
You can use Animated Text to 1.x but using this, will only work in OTC, in normal Tibia Client will crash.
Then i need to use Game.getSpectators to do what you asked for. But i think we should use Player:sendTextMessage.
With Game.getSpecatators, the text will be displayed to all players in area. We can change to only show to the player, just let me know.

Here is the code, i tryed to show how code works:
actions.xml:
XML:
<action itemid="7552" script="shell.lua"/>
<action itemid="7553" script="shell.lua"/>


shell.lua:
LUA:
--configuration table
local config = {
    cooldownTime = 72000, --time in seconds that player will be Exhausted
    cooldownStorage = 43000, --storage to hold the cooldown of Exhaustion
    pearlRewards = {7632, 7633}, --table with itemIds of the possible rewards
    closedShellItemId = 7552, --closed shell itemId
    openShellItemId = 7553, --open shell itemId
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if player:getStorageValue(config.cooldownStorage) <= os.time() and item.itemid == config.closedShellItemId then --Check if player has Exhaustion and itemid is a closed shell itemid
        local chance = math.random(100)--get a random number from 1 to 100
        local msg = ""
        if chance <= 16 then --finger 16% chance
            player:addHealth(-200)
            msg = "Ouch! You squeezed your fingers."
        elseif chance > 16 and chance <= 64 then --pearl 48% chance
            --select a random reward from pearlRewards table, looking from 1 index to last index
            local selectedReward = math.random(config.pearlRewards[1], config.pearlRewards[#config.pearlRewards])
            Game.createItem(selectedReward, 1, player:getPosition()) --create the selected reward at player position
            msg = "You found a beautiful pearl."
        else --nothing remaning 36% chance
            msg = "Nothing is inside."
        end
        local spectators = Game.getSpectators(item:getPosition(), false, true, 7, 7, 5, 5)--get all spectators in item view range
        for i = 1, #spectators do --for each spectator, show the text on item position
            spectators[i]:say(msg, TALKTYPE_MONSTER_SAY, false, spectators[i], item:getPosition())
        end
        item:transform(config.openShellItemId) --transform the closed shell in a open shell
        player:setStorageValue(config.cooldownStorage, os.time() + config.cooldownTime) --set Exhaustion to player
        return true
    else
        local spectators = Game.getSpectators(item:getPosition(), false, true, 7, 7, 5, 5)--get all spectators in item view range
        for i = 1, #spectators do --for each spectator, show the text on item position
            spectators[i]:say("You have already opened a shell today.", TALKTYPE_MONSTER_SAY, false, spectators[i], item:getPosition())
        end
        --Shouldn't you use the code below?
        --player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have already opened a shell today.")
        return false
    end
    return false
end

--Edit:
If you want to only show the text to the player above the shell, try this:
shell.lua
LUA:
--configuration table
local config = {
    cooldownTime = 72000, --time in seconds that player will be Exhausted
    cooldownStorage = 43000, --storage to hold the cooldown of Exhaustion
    pearlRewards = {7632, 7633}, --table with itemIds of the possible rewards
    closedShellItemId = 7552, --closed shell itemId
    openShellItemId = 7553, --open shell itemId
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if player:getStorageValue(config.cooldownStorage) <= os.time() and item.itemid == config.closedShellItemId then --Check if player has Exhaustion and itemid is a closed shell itemid
        local chance = math.random(100)--get a random number from 1 to 100
        local msg = ""
        if chance <= 16 then --finger 16% chance
            player:addHealth(-200)
            msg = "Ouch! You squeezed your fingers."
        elseif chance > 16 and chance <= 64 then --pearl 48% chance
            --select a random reward from pearlRewards table, looking from 1 index to last index
            local selectedReward = math.random(config.pearlRewards[1], config.pearlRewards[#config.pearlRewards])
            Game.createItem(selectedReward, 1, player:getPosition()) --create the selected reward at player position
            msg = "You found a beautiful pearl."
        else --nothing remaning 36% chance
            msg = "Nothing is inside."
        end
        player:say(msg, TALKTYPE_MONSTER_SAY, false, player, item:getPosition())
        item:transform(config.openShellItemId) --transform the closed shell in a open shell
        player:setStorageValue(config.cooldownStorage, os.time() + config.cooldownTime) --set Exhaustion to player
        return true
    else
        player:say("You have already opened a shell today.", TALKTYPE_MONSTER_SAY, false, player, item:getPosition())
        --Shouldn't you use the code below?
        --player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have already opened a shell today.")
        return false
    end
    return false
end
 
Last edited:
I've just found out that the open seashell has no message whatsoever, only the closed one, and that when you find a pear it displays some sort of bubbles effect:

ExDQyLx.gif
 
I've just found out that the open seashell has no message whatsoever, only the closed one, and that when you find a pear it displays some sort of bubbles effect:

ExDQyLx.gif
--Added bubble effects
--Removed msg that already opened a shell when its already open

Anything missing?
--Edited only the script that don't show the text to all spectators. Tell me which one are you using...
LUA:
--configuration table
local config = {
    cooldownTime = 72000, --time in seconds that player will be Exhausted
    cooldownStorage = 43000, --storage to hold the cooldown of Exhaustion
    pearlRewards = {7632, 7633}, --table with itemIds of the possible rewards
    closedShellItemId = 7552, --closed shell itemId
    openShellItemId = 7553, --open shell itemId
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == config.closedShellItemId then
        if player:getStorageValue(config.cooldownStorage) <= os.time() then --Check if player has Exhaustion and itemid is a closed shell itemid
            local chance = math.random(100)--get a random number from 1 to 100
            local msg = ""
            if chance <= 16 then --finger 16% chance
                player:addHealth(-200)
                msg = "Ouch! You squeezed your fingers."
            elseif chance > 16 and chance <= 64 then --pearl 48% chance
                --select a random reward from pearlRewards table, looking from 1 index to last index
                local selectedReward = math.random(config.pearlRewards[1], config.pearlRewards[#config.pearlRewards])
                Game.createItem(selectedReward, 1, player:getPosition()) --create the selected reward at player position
                msg = "You found a beautiful pearl."
            else --nothing remaning 36% chance
                msg = "Nothing is inside."
            end
            player:say(msg, TALKTYPE_MONSTER_SAY, false, player, item:getPosition())
            item:transform(config.openShellItemId) --transform the closed shell in a open shell
            player:setStorageValue(config.cooldownStorage, os.time() + config.cooldownTime) --set Exhaustion to player
            item:getPosition():sendMagicEffect(CONST_ME_BUBBLES) --for bubble effects
            return true
        else
            player:say("You have already opened a shell today.", TALKTYPE_MONSTER_SAY, false, player, item:getPosition())
            --Shouldn't you use the code below?
            --player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have already opened a shell today.")
            return false
        end
    end
    return false
end
 
Solution
Back
Top