• 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 Fruit Tree

Jfrye

Mapper, trying to learn scripting
Joined
Jan 8, 2009
Messages
366
Solutions
5
Reaction score
86
Location
Mexico Missouri
Im using tfs 1.1 on 10.77 client. I have managed to make a fruit tree script that suits my desire. There is one problem I cant seem to figure out though, so I am asking for help.

Code:
local chance = 25
local orangesAmmount = {1, 5}
local itemid = 4006
local timer = 10

function onUse(cid, item, toPosition, itemEx, fromPosition)
    if item.itemid == 4006 and (math.random(100) <= chance) then
        ammount = math.random(orangesAmmount[1], orangesAmmount[2])
        doPlayerAddItem(cid, 2675, ammount)
        doSendMagicEffect(fromPosition, CONST_ME_MAGIC_GREEN)
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You harvested " .. ammount .. " " ..(ammount>1 and "oranges" or "orange") .. " from the tree.")
        doTransformItem(item.uid, 4008)
        addEvent(doCreateItem, os.time() + 10, itemid, toPosition)
    else
        doSendMagicEffect(fromPosition, CONST_ME_POFF)
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You didn't harvest any oranges from this tree.")
        addEvent(function() doTransformItem(item.uid, 4006) end, timer * 1000)
    end
    return true
end

My problem seems to be that the trees do not always transform back to id 4006. Some of the trees will stay as id 4008. Is there anything I can change to make sure that they always transform back? Also, please ignore the timer numbers, as they were just put in place to test the script.

I forgot to add, that the trees will either not transform back, or they will instantly transform back, within a second of using it. Then ofcourse, some of them will work right.
 
Last edited:
Code:
addEvent(doCreateItem, os.time() + 10, itemid, toPosition)
should be changed to
Code:
addEvent(function() doTransformItem(item.uid, 4006) end, timer * 1000)
 
@SpiderOT, it seems to be working good now. Here is how the script currently looks.
Code:
local chance = 25
local orangesAmmount = {1, 5}
local timer = 10

function onUse(cid, item, toPosition, itemEx, fromPosition)
    if item.itemid == 4006 and (math.random(100) <= chance) then
        doAddCondition(cid, exhaust)
        ammount = math.random(orangesAmmount[1], orangesAmmount[2])
        doPlayerAddItem(cid, 2675, ammount)
        doSendMagicEffect(fromPosition, CONST_ME_MAGIC_GREEN)
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You harvested " .. ammount .. " " ..(ammount>1 and "oranges" or "orange") .. " from the tree.")
        doTransformItem(item.uid, 4008)
        addEvent(function() doTransformItem(item.uid, 4006) end, timer * 1000)       
    else
        doSendMagicEffect(fromPosition, CONST_ME_POFF)
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You didn't harvest any oranges from the tree.")
    end
    return true
end

Where would I add exhaustion between uses? Would I used a function such as doAddCondition(cid, exhaust)?
 
with better config, if it didn't work just copy the exhaust part xD
Code:
local cfg ={
chance = 25,
orangesAmmount = {1,5},
timer = 10,
tree = {4006,4008,2675},
exhaust = {3000,10}
}

function onUse(cid, item, toPosition, itemEx, fromPosition)
   if exhaustion.check(cid, cfg.exhaust[1]) == false then
     if item.itemid == cfg.tree[1] and (math.random(100) <= chance) then
       ammount = math.random(cfg.orangesAmmount[1], cfg.orangesAmmount[2])
       doPlayerAddItem(cid, cfg.tree[3], ammount)
       doSendMagicEffect(fromPosition, CONST_ME_MAGIC_GREEN)
       doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You harvested " .. ammount .. " " ..(ammount>1 and "oranges" or "orange") .. " from the tree.")
       doTransformItem(item.uid, cfg.tree[2])
       exhaustion.set(cid, cfg.exhaust[1], cfg.exhaust[2])
       addEvent(function() doTransformItem(item.uid, cfg.tree[1]) end, cfg.timer * 1000)
     else
       doSendMagicEffect(fromPosition, CONST_ME_POFF)
       doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You didn't harvest any oranges from the tree.")
     end
   else
     doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You must wait "..cfg.exhaust[2].." seconds between each harvest.")
     return false
   end
  return true
end
 
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/other/neworange.lua:onUse
data/action/scripts/other/neworange.lua:10: attempt to index global 'exhaustion' (a nil value)
stack traceback:
   [C]: in fucntion '_index'
   data/actions/scripts/other/neworange.lua:10: in function <data/actions/scripts/other/neworange.lua:9>

Thats the errors that show up in the server console when I try to use your script. So, that means that I am missing a file somewhere, right?

Edit, I added the function to global.lua, but now it throws a code related to this line : exhaustion.set(cid, exhausted)

I used this as local config : local exhausted = 10

So the script currently looks like this.
Code:
local chance = 25
local orangesAmmount = {1, 5}
local timer = 10
local exhausted = 10

function onUse(cid, item, toPosition, itemEx, fromPosition)
   if exhaustion.check(cid, exhaust) == false then
     if item.itemid == 4006 and (math.random(100) <= chance) then
       ammount = math.random(orangesAmmount[1], orangesAmmount[2])
       doPlayerAddItem(cid, 2675, ammount)
       doSendMagicEffect(fromPosition, CONST_ME_MAGIC_GREEN)
       doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You harvested " .. ammount .. " " ..(ammount>1 and "oranges" or "orange") .. " from the tree.")
       doTransformItem(item.uid, 4008)
       exhaustion.set(cid, exhausted)
       addEvent(function() doTransformItem(item.uid, 4006) end, timer * 1000)
     else
       doSendMagicEffect(fromPosition, CONST_ME_POFF)
       doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You didn't harvest any oranges from the tree.")
     end
   else
     doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You must wait "..exhausted.." seconds between each harvest.")
     return false
   end
  return true
end

I did it this way bc the way you sent it to me wouldnt do anything.
 
Last edited:
idon't know what tfs version you're using but anyway add this at the top of the script
Code:
local exhaustion =
{
   check = function (cid, storage)
     return getPlayerStorageValue(cid, storage) >= os.time()
   end,

   set = function (cid, storage, time)
     setPlayerStorageValue(cid, storage, os.time() + time)
   end

}

exhaust = {3000,10} <<<<< make sure 3000 is an empty storage, and 10 = 10 seconds
 
Back
Top