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

How to create an item and then remove it with certain time? (using addEvent)

Loremaster7

New Member
Joined
Oct 16, 2017
Messages
29
Reaction score
4
I created a lever, and I need it to disappear after 3 seconds ...

My code 1 :

Lua:
function onUse(cid, item, frompos, item2, topos)

time = 3

lever = {x = 2892, y = 2687, z = 8, stackpos=1}

getlever = getThingfromPos(lever)

if item.uid == 60115 and item.itemid == 1945 then
doTransformItem(item.uid,item.itemid+1)

addEvent(doCreateItem, 1945, doRemoveItem, (time*1000), 1945, 1, lever)

elseif item.uid == 60115 and item.itemid == 1946 then

doRemoveItem(getlever.uid,1945)
doTransformItem(item.uid,item.itemid-1)

end
return true
end


My code 2:

Lua:
function onUse(cid, item, frompos, item2, topos)

time = 3

lever = {x = 2892, y = 2687, z = 8, stackpos=1}

getlever = getThingfromPos(lever)

if item.uid == 60115 and item.itemid == 1945 then
doTransformItem(item.uid,item.itemid+1)

doCreateItem(1945,1,lever)

addEvent(doRemoveItem, (time*1000), 1945, 1, lever)

elseif item.uid == 60115 and item.itemid == 1946 then

doRemoveItem(getlever.uid,1945)
doTransformItem(item.uid,item.itemid-1)

end
return true
end

The 2 do not work.

Practically, what I need is the opposite of this:

18cF7Uw.gif


Instead of removing the stone, and it coming back in 3 seconds, I wanted to create a stone, and disappear it after 3 seconds ...
 
Last edited:
Solution
I created a lever, and I need it to disappear after 3 seconds ...

My code 1 :

Code:
function onUse(cid, item, frompos, item2, topos)

time = 3

lever = {x = 2892, y = 2687, z = 8, stackpos=1}

getlever = getThingfromPos(lever)

if item.uid == 60115 and item.itemid == 1945 then
doTransformItem(item.uid,item.itemid+1)

addEvent(doCreateItem, 1945, doRemoveItem, (time*1000), 1945, 1, lever)

elseif item.uid == 60115 and item.itemid == 1946 then

doRemoveItem(getlever.uid,1945)
doTransformItem(item.uid,item.itemid-1)

end
return true
end


My code 2:

Code:
function onUse(cid, item, frompos, item2, topos)

time = 3

lever = {x = 2892, y = 2687, z = 8, stackpos=1}

getlever = getThingfromPos(lever)

if item.uid == 60115 and item.itemid ==...
I created a lever, and I need it to disappear after 3 seconds ...

My code 1 :

Code:
function onUse(cid, item, frompos, item2, topos)

time = 3

lever = {x = 2892, y = 2687, z = 8, stackpos=1}

getlever = getThingfromPos(lever)

if item.uid == 60115 and item.itemid == 1945 then
doTransformItem(item.uid,item.itemid+1)

addEvent(doCreateItem, 1945, doRemoveItem, (time*1000), 1945, 1, lever)

elseif item.uid == 60115 and item.itemid == 1946 then

doRemoveItem(getlever.uid,1945)
doTransformItem(item.uid,item.itemid-1)

end
return true
end


My code 2:

Code:
function onUse(cid, item, frompos, item2, topos)

time = 3

lever = {x = 2892, y = 2687, z = 8, stackpos=1}

getlever = getThingfromPos(lever)

if item.uid == 60115 and item.itemid == 1945 then
doTransformItem(item.uid,item.itemid+1)

doCreateItem(1945,1,lever)

addEvent(doRemoveItem, (time*1000), 1945, 1, lever)

elseif item.uid == 60115 and item.itemid == 1946 then

doRemoveItem(getlever.uid,1945)
doTransformItem(item.uid,item.itemid-1)

end
return true
end

The 2 do not work.

Practically, what I need is the opposite of this:

18cF7Uw.gif


Instead of removing the stone, and it coming back in 3 seconds, I wanted to create a stone, and disappear it after 3 seconds ...
I'm not the best at older tfs version but I hope this helps.

Some notes:
  • Your tabbing could use some work. It can really improve the readability of your scripts.
  • You can put things that stay static locally outside of the function because there's no need to recreate them every time the function is called.
  • If you want to remove an item it's always a good idea to verify that it actually exists before doing so. Otherwise it could cause a crash, at least in later TFS versions.

Lua:
local time = 3
local lever_pos = {x = 2892, y = 2687, z = 8, stackpos=1}

function onUse(cid, item, frompos, item2, topos)
    local lever = getThingfromPos(lever_pos)
    if item.uid == 60115 and item.itemid == 1945 then
        doTransformItem(item.uid,item.itemid+1)
        doCreateItem(1945, 1, lever_pos)
        addEvent(function()
            local lever = getThingfromPos(lever_pos)
            if lever then
                doRemoveItem(lever.uid, 1945)
            end
        end, time * 1000)
    elseif item.uid == 60115 and item.itemid == 1946 then
        if lever then
            doRemoveItem(lever.uid, 1945)
        end
        doTransformItem(item.uid, item.itemid - 1)
    end
    return true
end
 
Solution
I'm not the best at older tfs version but I hope this helps.

Some notes:
  • Your tabbing could use some work. It can really improve the readability of your scripts.
  • You can put things that stay static locally outside of the function because there's no need to recreate them every time the function is called.
  • If you want to remove an item it's always a good idea to verify that it actually exists before doing so. Otherwise it could cause a crash, at least in later TFS versions.
Lua:
local time = 3
local lever_pos = {x = 2892, y = 2687, z = 8, stackpos=1}

function onUse(cid, item, frompos, item2, topos)
    local lever = getThingfromPos(lever_pos)
    if item.uid == 60115 and item.itemid == 1945 then
        doTransformItem(item.uid,item.itemid+1)
        doCreateItem(1945, 1, lever_pos)
        addEvent(function()
            local lever = getThingfromPos(lever_pos)
            if lever then
                doRemoveItem(lever.uid, 1945)
            end
        end, time * 1000)
    elseif item.uid == 60115 and item.itemid == 1946 then
        if lever then
            doRemoveItem(lever.uid, 1945)
        end
        doTransformItem(item.uid, item.itemid - 1)
    end
    return true
end

Muito Obrigado Apolo, funcionou perfeitamente!
 
Back
Top