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

[Help] Adding a timer event into a lua action

Oxious

Amateur Mapper & Scripter
Joined
Jan 25, 2009
Messages
89
Reaction score
0
Location
Germany
Greetings everyone!

I've become a little rusty when it comes to scripting, haven't done it in years. I tried the search function but didn't really find what I'm looking for. Tried a lot of things but I just can't get it to work the way I want.

The basic situation:

You all know the demon helmet quest. You go through a teleport and there's a switch on the right. If you pull this switch a teleport appears (to get out again) and a stone on the left disappears (so you can get the rewards).

What I have is:

I managed to create a script that lets a teleport appear and the stone disappear when you hit the lever. You can reverse this by flipping back the switch.

What I want though:

Is this whole thing to be timed. If you go down and hit the switch the teleport appears and the stone disappears. There the play should NOT be able to flip back the switch. Instead it should flip back itself (removing the teleport and placing the stone) after a certain ammount of time. After it automatically flipped back you can ofcourse use it again (starting the whole timer loop again).

The script I have so far is as thus:

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

local 	pinkstonepos1 = {x=634, y=664, z=13, stackpos=1}

local	timeToRemove = 5 --in seconds
local	TeleportToPos = { x = 645, y = 660, z = 12, stackpos = 1 }
local	TeleportInPos = { x = 636, y = 662, z = 13, stackpos = 1 }
local	LeverPos = { x = 647, y = 663, z = 13, stackpos = 1 }

local 	pinkstone1 = getThingfromPos(pinkstonepos1)
local	Teleport = getThingfromPos(TeleportInPos)
local	Lever = getThingfromPos(LeverPos)


function doRemoveTeleport()

		if Teleport.itemid == 1387 then
		doRemoveItem(getThingfromPos(TeleportInPos).uid)
		doSendMagicEffect(TeleportInPos, CONST_ME_POFF)
end
end


if item.itemid == 1945 then

	doRemoveItem(pinkstone1.uid, 1)
	doCreateTeleport(1387, TeleportToPos, TeleportInPos)
	doSendMagicEffect(TeleportInPos, CONST_ME_TELEPORT)
	doPlayerSendTextMessage(cid,22,"Click!")


elseif item.itemid == 1946 then

	doCreateItem(1355, 1, pinkstonepos1)
	doRemoveTeleport(1387, 1, Teleport)
	doPlayerSendTextMessage(cid,22,"Click!")

end

end


And by the way, I'm using TFS 0.3 beta3 if this is of any importance.

It is probably easy as hell but like I mentioned I'm a bit rusty. Any help is appreciated. :)


[EDIT]

Thanks to the people posting their suggestions here (you're getting reps) I managed to get the code to fully work the way I wanted it to. This is what it looks like now:

PHP:
local pinkstonepos1 = {x=634, y=664, z=13}
local TeleportToPos = { x = 645, y = 660, z = 12}
local TeleportInPos = { x = 636, y = 662, z = 13}
local LeverPos = { x = 647, y = 663, z = 13}
local timeToRemove = 5 --in seconds

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

local pinkstone1 = getTileItemById(pinkstonepos1, 1355)
local teleport = getTileItemById(TeleportInPos, 1387)

    	if item.itemid == 1945 then

        	doRemoveItem(pinkstone1.uid, 1)
        	doCreateTeleport(1387, TeleportToPos, TeleportInPos)
        	doSendMagicEffect(TeleportInPos, CONST_ME_TELEPORT)
        	doPlayerSendTextMessage(cid,22,"Click!")
        	addEvent(doRemoveTeleport, timeToRemove * 1000)

	else if item.itemid == 1946 then

		doPlayerSendCancel(cid,"Sorry, not possible.")

	return 1

	end
end  

end

function doRemoveTeleport()

local pinkstone1 = getTileItemById(pinkstonepos1, 1355)
local teleport = getTileItemById(TeleportInPos, 1387)
local Lever = getTileItemById(LeverPos, 1946)

    if teleport.uid > 0 then

        doCreateItem(1355, 1, pinkstonepos1)
        doRemoveItem(teleport.uid)
        doSendMagicEffect(TeleportInPos, CONST_ME_POFF)
        doTransformItem(Lever.uid, 1945)

    end

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

local config = 
{ 
 pinkstonepos1 = {x=634, y=664, z=13, stackpos=1},
 TeleportToPos = { x = 645, y = 660, z = 12, stackpos = 1 },
 TeleportInPos = { x = 636, y = 662, z = 13, stackpos = 1 },
 LeverPos = { x = 647, y = 663, z = 13, stackpos = 1 },
 pinkstone1 = getThingfromPos(pinkstonepos1),
 Teleport = getThingfromPos(TeleportInPos),
 Lever = getThingfromPos(LeverPos),
 thetime = 1000*60*30
}

function RemoveTeleport()
		doRemoveItem(getThingfromPos(config.TeleportInPos).uid)
		doSendMagicEffect(config.TeleportInPos, CONST_ME_POFF)
end

if item.itemid == 1945 then
	doRemoveItem(config.pinkstone1.uid, 1)
	doCreateTeleport(1387, config.TeleportToPos, config.TeleportInPos)
	doSendMagicEffect(config.TeleportInPos, CONST_ME_TELEPORT)
	doPlayerSendTextMessage(cid,22,"Click!")
elseif item.itemid == 1946 then
	doCreateItem(1355, 1, config.pinkstonepos1)
	addEvent(config.RemoveTeleport, config.thetime, cid)
	doPlayerSendTextMessage(cid,22,"Click!")
end
end

REP++? :>
 
Thanks for your help. I get this, though.

Code:
[25/01/2009 17:58:40] Reloaded actions.

[25/01/2009 17:58:43] Lua Script Error: [Action Interface] 
[25/01/2009 17:58:43] data/actions/scripts/quests/demonquest.lua:onUse

[25/01/2009 17:58:43] attempt to index a nil value
[25/01/2009 17:58:43] stack traceback:
[25/01/2009 17:58:43] 	[C]: in function 'getThingfromPos'
[25/01/2009 17:58:43] 	data/actions/scripts/quests/demonquest.lua:9: in function <data/actions/scripts/quests/demonquest.lua:1>

[25/01/2009 17:58:46] Lua Script Error: [Action Interface] 
[25/01/2009 17:58:46] data/actions/scripts/quests/demonquest.lua:onUse

[25/01/2009 17:58:46] attempt to index a nil value
[25/01/2009 17:58:46] stack traceback:
[25/01/2009 17:58:46] 	[C]: in function 'getThingfromPos'
[25/01/2009 17:58:46] 	data/actions/scripts/quests/demonquest.lua:9: in function <data/actions/scripts/quests/demonquest.lua:1>

I'll see if I can figure it out.
 
Thanks for your help. I get this, though.

Code:
[25/01/2009 17:58:40] Reloaded actions.

[25/01/2009 17:58:43] Lua Script Error: [Action Interface] 
[25/01/2009 17:58:43] data/actions/scripts/quests/demonquest.lua:onUse

[25/01/2009 17:58:43] attempt to index a nil value
[25/01/2009 17:58:43] stack traceback:
[25/01/2009 17:58:43] 	[C]: in function 'getThingfromPos'
[25/01/2009 17:58:43] 	data/actions/scripts/quests/demonquest.lua:9: in function <data/actions/scripts/quests/demonquest.lua:1>

[25/01/2009 17:58:46] Lua Script Error: [Action Interface] 
[25/01/2009 17:58:46] data/actions/scripts/quests/demonquest.lua:onUse

[25/01/2009 17:58:46] attempt to index a nil value
[25/01/2009 17:58:46] stack traceback:
[25/01/2009 17:58:46] 	[C]: in function 'getThingfromPos'
[25/01/2009 17:58:46] 	data/actions/scripts/quests/demonquest.lua:9: in function <data/actions/scripts/quests/demonquest.lua:1>

I'll see if I can figure it out.

try using a different stack position, or make sure your locations are right
 
The script I posted on the first post worked with exactly those coordinates and stackpos, so that can't be it.

Has it got something to do with the getThingfromPos values being inside the local config now?

I tried adding "config." before the values but that did not work either (same error message).
 
The script I posted on the first post worked with exactly those coordinates and stackpos, so that can't be it.

Has it got something to do with the getThingfromPos values being inside the local config now?

I tried adding "config." before the values but that did not work either (same error message).

sorry i had a moment :)
 
Last edited:
PHP:
local pinkstonepos1 = {x=634, y=664, z=13}

local timeToRemove = 5 --in seconds
local TeleportToPos = { x = 645, y = 660, z = 12}
local TeleportInPos = { x = 636, y = 662, z = 13}
local LeverPos = { x = 647, y = 663, z = 13}

function doRemoveTeleport()
	local teleport = getTileItemById(TeleportInPos, 1387)
	if teleport.uid > 0 then
		doCreateItem(1355, 1, pinkstonepos1)
		doRemoveItem(teleport.uid)
		doSendMagicEffect(TeleportInPos, CONST_ME_POFF)
		
		local Lever = getTileItemById(LeverPos, 1946)
		doTransformItem(Lever.uid, 1945)
	end
end

function onUse(cid, item, frompos, item2, topos)
	local pinkstone1 = getTileItemById(pinkstonepos1, 1355)
	local Teleport = getTileItemById(TeleportInPos, 1387)

	if item.itemid == 1945 then
		doRemoveItem(pinkstone1.uid, 1)
		doCreateTeleport(1387, TeleportToPos, TeleportInPos)
		doSendMagicEffect(TeleportInPos, CONST_ME_TELEPORT)
		doPlayerSendTextMessage(cid,22,"Click!")
		doTransformItem(item.uid, 1946)
		addEvent(doRemoveTeleport, TimeToRemove * 1000)
	end
end
 
Last edited:
Nope, same error.

Code:
[25/01/2009 19:57:47] attempt to index a nil value
[25/01/2009 19:57:47] stack traceback:
[25/01/2009 19:57:47] 	[C]: in function 'getThingfromPos'
[25/01/2009 19:57:47] 	data/actions/scripts/quests/demonquest.lua:9: in function <data/actions/scripts/quests/demonquest.lua:1>

Does that mean the function "getThingfromPos" was trying to call a value in row 9 of the .lua script but it turned out the value was nil?

If so, I don't get how that is possible. It seems to me that there is nothing wrong in row 9.
 
it means that you had sent a nil value to the getThingfromPos function at line 9...

ignore it and try my version...
 
Code:
[25/01/2009 20:04:52] Lua Script Error: [Action Interface] 
[25/01/2009 20:04:52] data/actions/scripts/quests/demonquest.lua:onUse

[25/01/2009 20:04:52] data/actions/scripts/quests/demonquest.lua:27: attempt to perform arithmetic on global 'TimeToRemove' (a nil value)
[25/01/2009 20:04:52] stack traceback:
[25/01/2009 20:04:52] 	data/actions/scripts/quests/demonquest.lua:27: in function <data/actions/scripts/quests/demonquest.lua:17>

o_O
 
Code:
[25/01/2009 20:15:27] Lua Script Error: [Action Interface] 
[25/01/2009 20:15:27] in a timer event called from: 
[25/01/2009 20:15:27] data/actions/scripts/quests/demonquest.lua:onUse

[25/01/2009 20:15:27] attempt to index a nil value
[25/01/2009 20:15:27] stack traceback:
[25/01/2009 20:15:27] 	[C]: in function 'getTileItemById'
[25/01/2009 20:15:27] 	data/actions/scripts/quests/demonquest.lua:9: in function <data/actions/scripts/quests/demonquest.lua:8>

This one happens after the 5 seconds when the teleport is supposed to disappear again.

[Edit] didn't post the complete error code.
 
Didn't work either. But I got it working now by modifying the suggestion you posted before.

This is what the code looks like now:

PHP:
local pinkstonepos1 = {x=634, y=664, z=13}
local TeleportToPos = { x = 645, y = 660, z = 12}
local TeleportInPos = { x = 636, y = 662, z = 13}
local LeverPos = { x = 647, y = 663, z = 13}
local timeToRemove = 5 --in seconds

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

local pinkstone1 = getTileItemById(pinkstonepos1, 1355)
local teleport = getTileItemById(TeleportInPos, 1387)

    	if item.itemid == 1945 then

        	doRemoveItem(pinkstone1.uid, 1)
        	doCreateTeleport(1387, TeleportToPos, TeleportInPos)
        	doSendMagicEffect(TeleportInPos, CONST_ME_TELEPORT)
        	doPlayerSendTextMessage(cid,22,"Click!")
        	addEvent(doRemoveTeleport, timeToRemove * 1000)

	else if item.itemid == 1946 then

		doPlayerSendCancel(cid,"Sorry, not possible.")

	return 1

	end
end  

end

function doRemoveTeleport()

local pinkstone1 = getTileItemById(pinkstonepos1, 1355)
local teleport = getTileItemById(TeleportInPos, 1387)
local Lever = getTileItemById(LeverPos, 1946)

    if teleport.uid > 0 then

        doCreateItem(1355, 1, pinkstonepos1)
        doRemoveItem(teleport.uid)
        doSendMagicEffect(TeleportInPos, CONST_ME_POFF)
        doTransformItem(Lever.uid, 1945)

    end

end

You hit the lever from left to right; the teleport appears and the stone disappears for 5 seconds; then the stone appears and the teleport disappears; the lever flips back from right to left; while the lever is flipped to the right you cannot move it.

Thanks alot for your suggestions! :D
 
ehh and why didn't it work? :)

i am 100% sure it did work :D atleast no errors, maybe it didnt do what it was supposed to but yeah ;)

+ in function doRemoveTeleport you declared variable "pinkstone1", remove it, cuz it aint used :p
 
Hehe, I removed that line, thanks for the tip.

I posted the script in the main forum for actionscripts. Of course you guys are credited. :)

By the way, is reputation turned off? I can't seem to find the button to do so. :S
 
Last edited:
Back
Top