• 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 [Spell/Buff] How to use AddEvent properly so it functions as a "timer" for spells?

Guitar Freak

_LüA_n☺b_
Joined
Dec 27, 2008
Messages
831
Reaction score
13
Location
Caracas, Venezuela
So Im back with another question that I cant seem to solve by myself :(

I never really learned how to use AddEvent properly so I have no idea how to get this going the way I want.

Basically I want it so my "spell" is a timed buff. You would think "Why dont you do it with the normal CONDITION stuff for haste/skills/etc?" well, this buff works with a storage value so I dont think "conditions" apply for this.

So again this is where Im stuck and where I need help:

  1. Player uses the spell, player receives a storage value for X amount of seconds, and after those X amount of seconds had expired, the storage value is removed again.

  2. If possible, when the buff is applied, an animation (doSendAnimatedText(pos, text, color)) shows the countdown of the timer, so if the timer is 3 it would show in a 1 sec delay "loop": 3, 2, 1 then simply stop.


I think I can actually do the second one myself if I receive help understanding the first, but I posted it just in case.

Thanks in advance and as always I +rep anyone who is helpful, even if it isnt worth much :)

PD: Im using TFS 0.3.5pl1, in case you needed this information.
 
Post your code since I don't get how the storage is needed for that buff.

It is of this sort:

Lua:
function onUse(cid, item, frompos, item2, topos)
     doPlayerSetStorageValue(item2.uid, whatever_storage, (getPlayerStorageValue(item2.uid, whatever_storage) + whatever_value)
     return true
end

So this whatever_storage is actually "something" that has a value lets say 10, so when the "buff" is applied (onUse) I want it so it goes 10+whatever_value but only for X seconds then go back to 10.

Also, notice the onUse and item2.uid, this is actually a rune made on actions so to prevent any confusion it doesnt "necessarily" need to be a "spell", can be a talkaction/action/anything, I just need to understand how AddEvent would work for this.
 
Last edited:
This is not a "request" Tosuxo, it is a call for help.

Im trying to get someone to explain me how to use AddEvent properly to do what I posted on that list, not someone who will give me the script ready.

Thanks for trying but please read the full post next time before posting.

Peace.

it's a request for code. simple.
 
Code:
local function loop(cid, n)
	n = n - 1
	if isPlayer(cid) then
		if n > 0 then
			doSendAnimatedText(getThingPosition(cid), n, COLOR_WHITE)
			addEvent(loop, 1000, cid, n)
		else
			doCreatureSetStorage(cid, XXX, -1)
		end
	end
end

function onCastSpell(cid, var)
	doCreatureSetStorage(cid, xxx, 1)
	addEvent(loop, 1000, cid, yyy)
	return true
end
 
it's a request for code. simple.

A request for code is:

"OMFG PLZ SOMUN11 MAKEME SCRIPZ FOR WHEN PLAYER GO TOILET HE POOPS AND HE GAINZ PREMMUM!!!"

Maybe I shouldve made my first post clearer, but in conclusion, this is not that. This is a request for help. Im not asking for a CODE, Im not asking for a SCRIPT, Im asking for an EXPLANATION. I made this thread to understand, not to leech. If the EXPLANATION comes with an example script which is usually likely, that's up to the person, but the request wasnt for it or the script itself.

If you want to keep not understanding, not helping, and trying to prove a point that never existed, go ahead, but I rest my case.

@EvulMastah:

I cant test the script right now to see how does everything work but at first glance there are a few things Im unsure of:

1) Why "n = n - 1"? Is this refering to the fact that storage values come as -1 by default or it has nothing to do with that and is something else?
2) On the last addEvent(loop, 1000, cid, yyy) what exactly is the yyy? The "buff timer" of my example or something else?
3) Im a bit confused about understanding this part:

Lua:
		if n > 0 then -- Guessing this is if the storage is > 0?
			doSendAnimatedText(getThingPosition(cid), n, COLOR_WHITE) -- n = timer?
			addEvent(loop, 1000, cid, n) -- n = timer? || Also, this means the Event will "start" happening every 1000ms for n amount of time? I kinda thought "something" was needed to "break" the event (when using addEvent) in order to "stop it" for it not to infinitely loop itself, so I guess I had this wrong?
		else
			doCreatureSetStorage(cid, XXX, -1) -- why setting it to -1? 
		end

The doubts I have are in the script as -- "comments" for every independent line.
Thanks for helping anyway!
+Repped
 
1) Why "n = n - 1"? Is this refering to the fact that storage values come as -1 by default or it has nothing to do with that and is something else?
n is a number which the iterator (function loop) uses to determine how many iterations / loops / repeats are left. They have to be decreased by 1 on every iteration.
2) On the last addEvent(loop, 1000, cid, yyy) what exactly is the yyy? The "buff timer" of my example or something else?
How many times do you want it to iterate. It is referred to as "n" in the final function.
3) Im a bit confused about understanding this part:
Lua:
if n > 0 then -- Guessing this is if the storage is > 0?
No, it checks if there are still iterations to perform.
Lua:
doSendAnimatedText(getThingPosition(cid), n, COLOR_WHITE) -- n = timer?
As explained above. It sends the current number value/iteration.
Lua:
addEvent(loop, 1000, cid, n) -- n = timer? || Also, this means the Event will "start" happening every 1000ms for n amount of time? I kinda thought "something" was needed to "break" the event (when using addEvent) in order to "stop it" for it not to infinitely loop itself, so I guess I had this wrong?
In case there are still iterations to perform, this keeps the iterator alive. The decrement (n = n - 1) eventually "breaks"/stops it.
Lua:
doCreatureSetStorage(cid, XXX, -1) -- why setting it to -1?
To erase it. It would be smarter to ommit the ", -1", as it would be truly deleted from the database then, and not just set to -1.
 
Back
Top