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

Need a hand with this script (TFS 1.2)

FenX

Advanced OT User
Joined
Jul 8, 2019
Messages
360
Solutions
1
Reaction score
159
Hello everyone,

Started to play around and made a little script:
Lua:
local function revertSack(position, itemId, transformId)
    local item = Tile(position):getItemById(itemId)
    if item then
            item:transform(transformId)
        end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local rand = math.random(1, 100)
    if item.itemid == 7537 then
        if rand <= 20 then
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You search through the egg sack and manage to find some tarantula eggs.')
            player:addItem(11198, 1)
            item:transform(7536, 7537)
            addEvent(revertSack, 120000, toPosition, 7536, 7537)
            toPosition:sendMagicEffect(3)
        else
        if rand <= 40 then
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'Something nips your hand as you attempt to search through the egg sack.')
            item:transform(7536, 7537)
            addEvent(revertSack, 120000, toPosition, 7536, 7537)
            toPosition:sendMagicEffect(3)
            doSummonCreature("Tarantula", toPosition)
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'A spider crawls out of the egg sack as you reach inside.')
            item:transform(7536, 7537)
            addEvent(revertSack, 120000, toPosition, 7536, 7537)
            toPosition:sendMagicEffect(3)
            doSummonCreature("Spider", toPosition)
        end
    end
end
Getting an error like this:
[Warning - Event::checkScript] Can not load script: scripts/spidersack.lua
data/actions/scripts/spidersack.lua:44: 'end' expected (to close 'function' at line 18) near '<eof>'

All I've done is edited an already existing script which worked fine however, my edit doesn't work 😅.
Could anybody explain to me where I messed up? Would be grateful!

Ty.
 
Solution
You are missing one end
Lua:
local function revertSack(position, itemId, transformId)
    local item = Tile(position):getItemById(itemId)
    if item then
            item:transform(transformId)
        end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local rand = math.random(1, 100)
    if item.itemid == 7537 then
        if rand <= 20 then
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You search through the egg sack and manage to find some tarantula eggs.')
            player:addItem(11198, 1)
            item:transform(7536, 7537)
            addEvent(revertSack, 120000, toPosition, 7536, 7537)
            toPosition:sendMagicEffect(3)
        else
        if rand <= 40 then...
You are missing one end
Lua:
local function revertSack(position, itemId, transformId)
    local item = Tile(position):getItemById(itemId)
    if item then
            item:transform(transformId)
        end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local rand = math.random(1, 100)
    if item.itemid == 7537 then
        if rand <= 20 then
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You search through the egg sack and manage to find some tarantula eggs.')
            player:addItem(11198, 1)
            item:transform(7536, 7537)
            addEvent(revertSack, 120000, toPosition, 7536, 7537)
            toPosition:sendMagicEffect(3)
        else
        if rand <= 40 then
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'Something nips your hand as you attempt to search through the egg sack.')
            item:transform(7536, 7537)
            addEvent(revertSack, 120000, toPosition, 7536, 7537)
            toPosition:sendMagicEffect(3)
            doSummonCreature("Tarantula", toPosition)
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'A spider crawls out of the egg sack as you reach inside.')
            item:transform(7536, 7537)
            addEvent(revertSack, 120000, toPosition, 7536, 7537)
            toPosition:sendMagicEffect(3)
            doSummonCreature("Spider", toPosition)
        end
    end
end
end
 
Solution
Pfft. Thanks haha🤦‍♀️

@M0ustafa Apologise for double posting.

Just a quick question, how would I randomise the amount of the specified item the player receives? E.g. say I wanted for player to receive between 1-5 of the itemid used?
 
I am just getting started with Lua and scripting Tibia, but changing this

Code:
player:addItem(11198, 1)

to

Code:
player:addItem(11198, math.random(1, 5))

Should do it, I think. Assuming using math.random(lower, upper) returns whole numbers. If it doesn't you will need to round that.
 
I am just getting started with Lua and scripting Tibia, but changing this

Code:
player:addItem(11198, 1)

to

Code:
player:addItem(11198, math.random(1, 5))

Should do it, I think. Assuming using math.random(lower, upper) returns whole numbers. If it doesn't you will need to round that.

I see, thanks m8 :)
 
Lua:
local function revertSack(position, itemId, transformId)
    local item = Tile(position):getItemById(itemId)
    if item then
            item:transform(transformId)
        end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    -- if item.itemid == 7537 then You don't need to check the itemid because it is the only itemid linked to the script. It is defined in actions.xml. --
    --If you had more than 1 item linked to this code then you would need to specify which item so you can delete all the green text in this code --
   
        --Next we want the code to be clean so lets see how we can optimize it.--
   
        -- The first thing is all of them have the methods: player:sendTextMessage(), item:transform(), sendMagicEffect, and addEvent. If we code it correctly we can use these lines 1x --
       
        -- Lets define a value for the text so we can use the sendTextMessage once in the code. --
        local text = ""
        local rand = math.random(1, 100)
       
        if rand <= 20 then
            text = 'You search through the egg sack and manage to find some tarantula eggs.' -- We replace the sendTextMessage with defining the text.--
            player:addItem(11198, math.random(1, 5)) --Here you wanted to make the item a random amount the math.random(1, 5) is how. It will randomly give 1-5 items--
            --We keep player:addItem() because its not used in all cases. So it should only happen here.--
            --We moved item:transform() to the bottom of the code because the whole code always runs. There is no time the code stops so we can put any methods we use in all cases after the cases. (if, elseif, else)--
            --We also move addEvent() to the bottom for the same reason--
            --We also do it for sendMagicEffect--
        elseif rand <= 40 then -- Now we do the same for this case --
            text = 'Something nips your hand as you attempt to search through the egg sack.'
            --This time we remove item:transform(), addEvent(), and sendMagicEffect() sense the code will do it later on--
            doSummonCreature("Tarantula", toPosition) -- This method isn't in all cases so we keep it --
        else -- Now the same for the last case
            text = 'A spider crawls out of the egg sack as you reach inside.'
            doSummonCreature("Spider", toPosition) -- This method isn't in all cases so we keep it --
        end
       
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, text)
        item:transform(7536, 7537)
        toPosition:sendMagicEffect(3)
        addEvent(revertSack, 120000, toPosition, 7536, 7537)
    return true
end

Final code
Lua:
local function revertSack(position, itemId, transformId)
    local item = Tile(position):getItemById(itemId)
    if item then
            item:transform(transformId)
        end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
        local text = ""
        local rand = math.random(1, 100)
        
        if rand <= 20 then
            text = 'You search through the egg sack and manage to find some tarantula eggs.'
            player:addItem(11198, math.random(1, 5))
        elseif rand <= 40 then
            text = 'Something nips your hand as you attempt to search through the egg sack.'
            doSummonCreature("Tarantula", toPosition)
        else
            text = 'A spider crawls out of the egg sack as you reach inside.'
            doSummonCreature("Spider", toPosition)
        end
        
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, text)
        item:transform(7536, 7537)
        toPosition:sendMagicEffect(3)
        addEvent(revertSack, 120000, toPosition, 7536, 7537)
    return true
end
 
Lua:
local function revertSack(position, itemId, transformId)
    local item = Tile(position):getItemById(itemId)
    if item then
            item:transform(transformId)
        end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    -- if item.itemid == 7537 then You don't need to check the itemid because it is the only itemid linked to the script. It is defined in actions.xml. --
    --If you had more than 1 item linked to this code then you would need to specify which item so you can delete all the green text in this code --
  
        --Next we want the code to be clean so lets see how we can optimize it.--
  
        -- The first thing is all of them have the methods: player:sendTextMessage(), item:transform(), sendMagicEffect, and addEvent. If we code it correctly we can use these lines 1x --
      
        -- Lets define a value for the text so we can use the sendTextMessage once in the code. --
        local text = ""
        local rand = math.random(1, 100)
      
        if rand <= 20 then
            text = 'You search through the egg sack and manage to find some tarantula eggs.' -- We replace the sendTextMessage with defining the text.--
            player:addItem(11198, math.random(1, 5)) --Here you wanted to make the item a random amount the math.random(1, 5) is how. It will randomly give 1-5 items--
            --We keep player:addItem() because its not used in all cases. So it should only happen here.--
            --We moved item:transform() to the bottom of the code because the whole code always runs. There is no time the code stops so we can put any methods we use in all cases after the cases. (if, elseif, else)--
            --We also move addEvent() to the bottom for the same reason--
            --We also do it for sendMagicEffect--
        elseif rand <= 40 then -- Now we do the same for this case --
            text = 'Something nips your hand as you attempt to search through the egg sack.'
            --This time we remove item:transform(), addEvent(), and sendMagicEffect() sense the code will do it later on--
            doSummonCreature("Tarantula", toPosition) -- This method isn't in all cases so we keep it --
        else -- Now the same for the last case
            text = 'A spider crawls out of the egg sack as you reach inside.'
            doSummonCreature("Spider", toPosition) -- This method isn't in all cases so we keep it --
        end
      
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, text)
        item:transform(7536, 7537)
        toPosition:sendMagicEffect(3)
        addEvent(revertSack, 120000, toPosition, 7536, 7537)
    return true
end

Final code
Lua:
local function revertSack(position, itemId, transformId)
    local item = Tile(position):getItemById(itemId)
    if item then
            item:transform(transformId)
        end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
        local text = ""
        local rand = math.random(1, 100)
       
        if rand <= 20 then
            text = 'You search through the egg sack and manage to find some tarantula eggs.'
            player:addItem(11198, math.random(1, 5))
        elseif rand <= 40 then
            text = 'Something nips your hand as you attempt to search through the egg sack.'
            doSummonCreature("Tarantula", toPosition)
        else
            text = 'A spider crawls out of the egg sack as you reach inside.'
            doSummonCreature("Spider", toPosition)
        end
       
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, text)
        item:transform(7536, 7537)
        toPosition:sendMagicEffect(3)
        addEvent(revertSack, 120000, toPosition, 7536, 7537)
    return true
end

That's actually very helpful. I appreciate the time you took to do this, definitely makes it look a bit neater. <3
 
Got another question regarding this same script.

I've added
Lua:
doCreatureSay(cid, "Wow!", TALKTYPE_ORANGE_1)
to one of the lines

Now where in
Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
do I add cid for it to work?
 
Back
Top