• 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 Scripting tutorial(actions)

Fare

Advanced OT User
Joined
Apr 3, 2008
Messages
2,387
Reaction score
151
Location
Ukraine
In this tutorial I will generally explain how to work with action scripts.
  1. Useful information (read when you are new with this)
  2. Lua functions

----1 Useful information

When editing the .lua / .xml files the best editor to use is simply Notepad. Also it's very easy to use Notepad ++, scripting with this program is very easy, and comfortable. You can download notepad++ HERE

---1.1 Storing data

When scripting in ot action files you will have to understand what some thing mean. One of the important things are items in which you can store data. Here I will give an example:


Code:
number = 1
Now the storage item "number" contains "1". So each time we will call up the storage value "number", we will get the answer "1".

When you'd like to perform math calculations you can do it like this:

Code:
number = number + 1
Now there will be added 1 to the storage value number. You can replace + with -, / and some more math signs.
If you want number to be an random number it would be like this:

Code:
number=math.random(1,10)

1,10 is the min/max number that the random init should get.(math.random is a function)

It is also possible to store text inside these storage items. An example:

Code:
name = "Hi, my name is Fare"
Now the storage item "name" will contain the text "Hi, my name is Fare".

I highly recommend to use local in scripts. What they are supposed to? When announcing a value, (anything = 5), you creating a storage value which store "5", after script was executed, you don't need to keep this "5", becouse script was already executed. Thats why I recommend to use local values. The word ‘local’ means that the variable is only used only when executing script. As soon as the script is done, the memory is cleared again.

---1.2 "If" statements:
An if statement tests its condition and executes its then-part or its else-part accordingly. The else-part is optional.
Another words, "if" statement simply says that a piece of code between the "if" and the "end" will only be carried out when the lines of code between the "if" and the "then" is correct.
You always need to close an "if" statement with and "end".

Example:
Code:
if apple == 1 then
"piece of code"
end

This code will carry out the "piece of code" only if the storage value "apple" is 1. Look at the "==" part. When using an if statement and you want to compare two factors you must use the "=" signe twice instead of once!
If you want the "if" statement to compare multiple factors in one statement it will look like this:
Code:
if apple == 1 and pear == 2 then
"piece of code"
end

Now the "piece of code" will only be carried out when the storage value "apple" is 1 and "pear" is 2. Of course you can put as many factors in one if statement, as long as you separate them with an "and".
If you'd like to carry out "piece of code" when the storage value "apple" is 1 OR when the storage value "pear" is 2 simply replace the "and" by an "or".

Let's say you want to execute "piece of code1" when the storage value "apple" is 1, and "piece of code2" when the storage value "apple" is not 1. It will have to look like this:

Code:
if apple == 1 then
"piece of code"
else
"piece of code2"
end

You simply put an "else" between the "if ... then" and the "end". Everything which is between the "if .. then" and the "else" will be carried out if the storage value apple IS 1, everything which is between the "else" and "end" will be carried out when and store value apple is NOT 1.


Now, what if you want "piece of code1" to be carried out when the storage value "apple" is 1, and "piece of code2" when the storage value "apple" is 2. When apple is neither 1 nor 2 “piece of code3” will be carried out. Example:

Code:
if apple == 1 then
"piece of code1"
elseif apple == 2 then
"piece of code2"
else
"piece of code3"
end


If the storage value "apple" is 1 "piece of code1" will be carried out, but if the storage value "apple" is 2 ONLY "piece of code2" will be carried out!
When the apple is neither 1 nor 2 "piece of code 3" will be carried out.
You can put as many "elseif...then"'s between the "if...then" and the "else" as you want.
If you want nothing to happen when the apple is neither 1 or 2 simple remove the(else "piece of code3")


When you are using "==" between apple and 1(if apple == 1 then) you are comparing. So you say, if the storage value of "apple" is 1 then ....
When you use the "==" sign, the part in front of the "==" (apple) most of the time represents a storage item.
But it would be nice if we could do other things then comparing factors! Therefore we can place a few different things between the storage value and then thing you want to compare (or something else) it to.

Examples:

-
Code:
if apple > 1 then
(will only be carried out if the storage value "apple" is bigger then 1)
-
Code:
if apple < 1 then
(will only be carried out if the storage value "apple" is smaller then 1)
-
Code:
if apple ~= 1 then
(will only be carried out if the storage value "apple" is not 1)
-
Code:
if apple >= then
(will only be carried out if the storage value "apple" is 1, or bigger then 1)
-
Code:
if apple <= then
(will only be carried out if the storage value "apple" is 1, or smaller then 1)


---1.3 "For" statements

For statements are completly different then if statements. Here is an example:

Code:
for apple 1,10 do
doPlayerSendTextMessage(cid,18,apple)
end

Now, I will explain a bit.
"doPlayerSendTextMessage(cid,18,apple)" is an function (more about this later) which will send a message to the player with a text. Now the text which will be send is the contains of the storage item "apple". In this code the for statement will create a loop. It will repeat "doPlayerSendTextMessage(cid,18,apple)" until the storage value apple is 10.

Code:
for apple [B]nr1[/B],[B]nr2[/B] do
Here nr1 is the starting number, nr2 is the finishing number. The for loop will automatically increase the value of the storage item "apple" each time it repeats the loop.


---1.4 Repeat statements

Repeat statements will repeat a piece of code until something reaches a value. Example:


Code:
Repeat
“piece of code”
Until NumberOfApples > NumberOfPearsNow

the “piece of code” wil be repeated untill the storage value NumberOfApples is bigger then the storage value NumberOfPears. Ofcourse you can change NumberOfPears into a number.




Credits goes to Mindstorm!
I just found this tutorial and I think it can be usefull, so I updated, completed, and posted it here.
 
Last edited:
----2 Commands(lua functions)
When scripting action files there are a lot of lua functions which are useful to know. Here I will explain the once which you will use most.

function onUse(cid, item, fromPosition, itemEx, toPosition)”:
This is what every action script starts with. The function onUse() just means that you are using something (a item or whatever). There are a few "storage values" you are given in the action script, which you don’t have to set! These storage values are:
-cid: The person who uses the item (info about the person using).
-item: Info on the item he is using. (so if you ctrl-click on a blue berry bush a lot information about that blue berry bush is stored inside the item storage value).
-fromPosition: From wich position character is going to use the item.
-itemEx: if character is using the item on something, itemEx contains data of that item.
-toPosition: to which position character is using to the item.
Keep in mind that fuction onUse() always ends with an “end”

There are also some other events(based on TFS 0.3. Creaturescripts:
onLogin(cid) - this script executing when every player has loggin in.
onLogout(cid) - when every players logging out.
onJoinChannel(cid, channel, users) - when player joining any speak channel(trade,help, ect)
onLeaveChannel(cid, channel, users) - when player leaving speak channel.
onAdvance(cid, skill, oldLevel, newLevel) - when player get skill advance. If can be level, magic level, sword fighting, fishing, anything.
onSendMail(cid, receiver, item, openBox) - when player send mail ^^
onReceiveMail(cid, sender, item, openBox) - when player receive mail.
onLook(cid, position) - when player looks on anything(shift + click, or right + left mouse click)
onThink(cid, interval) - this type of event executing every interval time(used mostly in globalevents, with specific interval.
onStatsChange(cid, attacker, type, combat, value) - when players stats going to be changed(hp, mana, ect). Example:
Code:
 onStatsChange(cid, attacker, type, combat, value)
	if(type == STATSCHANGE_HEALTHGAIN and combat == COMBAT_HEALING and value >= 500)
		doPlayerSendTextMessage(cid, 22, "Woah, you've just healed for more than 500 hit points!")
	end
end
onAttack(cid, target) - every time when player attacking something.
onKill(cid, target) - every time when player killing something(target).
onDeath(cid, corpse, lastHitKiller, mostDamageKiller) - every time when player(or creature) dies.
onPrepareDeath(cid, lastHitKiller, mostDamageKiller) - this event going to be executed before player(or creature) dies, so, script can be executed before player(creature) dies. For example if you will add to player +1 hit point with onPrepareDeath event, players(or creatures) will be immortal.

Also, in next release of TFS 0.3 there will be such creature events like onCast, onCombat and onAreaCombat.


Here you can found lua functions list!

Here I will explain a small example script(it's party hat's script):

PHP:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	if item.uid == getPlayerSlotItem(cid, CONST_SLOT_HEAD).uid then
		doSendMagicEffect(getPlayerPosition(cid), CONST_ME_GIFT_WRAPS)
		return TRUE
	end
	return FALSE
end

function onUse(cid, item, fromPosition, itemEx, toPosition) - as you can see, this script going to be executed when player used the item.
getPlayerSlotItem(cid, slot) - This function will check what item player have actually in slot. This function returns array with item which is actually in slot. When slot is empty, then return = 0 (FALSE).
if item.uid == getPlayerSlotItem(cid, CONST_SLOT_HEAD).uid then - Now, we checking if the item which player used, is in players head slot. item.uid - item which player used. .uid means unique id of item. Every item got his own unique id, so it's not possible to have 2 SAME items, with same unique ids.
doSendMagicEffect(getPlayerPosition(cid), CONST_ME_GIFT_WRAPS) - This function(doSendMagicEffect(pos, type[, creature])) sends a magic effect to specific potision. getPlayerPosition(cid) returns array with players position. CONST_ME_GIFT_WRAPS type of effect, you can found all effect types in your lib\constant.lua file.
return TRUE - A return statement returns occasional results from a function or simply finishes a function. There is an implicit return at the end of any function, so you do not need to use one if your function ends naturally, without returning any value. So, when you return TRUE, you returning, what script was succes.


Here I will try to explain a more difficult script(I used surprise bag script):

PHP:
local bluePresent = {2687, 6394, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114}
local redPresent = {2152, 2152, 2152, 2153, 5944, 2112, 6568, 6566, 2492, 2520, 2195, 2114, 2114, 2114, 6394, 6394, 6576, 6576, 6578, 6578, 6574, 6574}

function onUse(cid, item, fromPosition, itemEx, toPosition)
	local count = 1
	if item.itemid == 6570 then
		local randomChance = math.random(1, 11)
		if randomChance == 1 then
			count = 10
		elseif randomChance == 2 then
			count = 3
		end
		doPlayerAddItem(cid, bluePresent[randomChance], count)
	elseif item.itemid == 6571 then
		local randomChance = math.random(1, 22)
		if randomChance > 0 and randomChance < 4 then
			count = 10
		end
		doPlayerAddItem(cid, redPresent[randomChance], count)
	end
	doSendMagicEffect(fromPosition, CONST_ME_GIFT_WRAPS)
	doRemoveItem(item.uid, 1)
	return TRUE
end

Lets start.

Code:
local bluePresent = {2687, 6394, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114}
local redPresent = {2152, 2152, 2152, 2153, 5944, 2112, 6568, 6566, 2492, 2520, 2195, 2114, 2114, 2114, 6394, 6394, 6576, 6576, 6578, 6578, 6574, 6574}
- we declaring 2 arrays(tables). First array called bluePresent and second called redPresent. In every array, we store some values, every value separated by comma.
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
- thats how we start every action script.(script executed when item was used)
Code:
local count = 1
- declaring local value called count, and storing value "1" there.
Code:
if item.itemid == 6570 then
- if item id(item "number") which we used equal to 6570, then we executing next part of script, if not then we going to execute "else" part of this "if" statement.
Code:
local randomChance = math.random(1, 11)
- declaring new storage value called randomChange, and storing there a random number between 1 and 11.
Code:
if randomChance == 1 then
			count = 10
		elseif randomChance == 2 then
			count = 3
		end
- checking our random number stored in value randomChance. If this value equal to 1 then we assign value "10" into storage called count. elseif randomChance == 2 then If, value randomChance is NOT 1, and equals to 2, then we assign value "3" into storage called count.
Code:
doPlayerAddItem(cid, bluePresent[randomChance], count)
- This function(doPlayerAddItem(uid, itemid[, count/subtype[, canDropOnMap]])) gives a specific item to player. So, in this part, we adding to player item, from array bluePresent. So, if, for example randomChance(which is random), equals to 4, then we adding to player item, equals to position 4 in array bluePresent(it's item id 6574).
Code:
doSendMagicEffect(fromPosition, CONST_ME_GIFT_WRAPS)
- I already explained how this function works.
Code:
doRemoveItem(item.uid, 1)
- This function (doRemoveItem(uid[, n])) removes an item. In this part we removing item which we used, so it makes our surprise bag dissapears. 1 means, the count of items, which we need to be removed.
 
Last edited:
I will try to make this tutorial more advaced, write down your suggestions.
I am sorry for my english, but at least I tryed, and hopefully this tutorial will help someone... ^^
 
Last edited:
Hm, it's also important to explain 'local' values in 1.1 storing data.
 
Seriously, you can do better.
And another thing, items with stored values may only be used without the local command inside the function :)
Nice tutorial anyways. Just explain a bit more about if statements.

\\
 
Thank you dude It was use full for a noob like me, you deserve rep. I hope I will see more tutorials from you.
 
----2 Commands(lua functions)
When scripting action files there are a lot of lua functions which are useful to know. Here I will explain the once which you will use most.

function onUse(cid, item, fromPosition, itemEx, toPosition)”:
This is what every action script starts with. The function onUse() just means that you are using something (a item or whatever). There are a few "storage values" you are given in the action script, which you don’t have to set! These storage values are:
-cid: The person who uses the item (info about the person using).
-item: Info on the item he is using. (so if you ctrl-click on a blue berry bush a lot information about that blue berry bush is stored inside the item storage value).
-fromPosition: From wich position character is going to use the item.
-itemEx: if character is using the item on something, itemEx contains data of that item.
-toPosition: to which position character is using to the item.
Keep in mind that fuction onUse() always ends with an “end”

There are also some other events(based on TFS 0.3. Creaturescripts:
onLogin(cid) - this script executing when every player has loggin in.
onLogout(cid) - when every players logging out.
onJoinChannel(cid, channel, users) - when player joining any speak channel(trade,help, ect)
onLeaveChannel(cid, channel, users) - when player leaving speak channel.
onAdvance(cid, skill, oldLevel, newLevel) - when player get skill advance. If can be level, magic level, sword fighting, fishing, anything.
onSendMail(cid, receiver, item, openBox) - when player send mail ^^
onReceiveMail(cid, sender, item, openBox) - when player receive mail.
onLook(cid, position) - when player looks on anything(shift + click, or right + left mouse click)
onThink(cid, interval) - this type of event executing every interval time(used mostly in globalevents, with specific interval.
onStatsChange(cid, attacker, type, combat, value) - when players stats going to be changed(hp, mana, ect). Example:
Code:
 onStatsChange(cid, attacker, type, combat, value)
	if(type == STATSCHANGE_HEALTHGAIN and combat == COMBAT_HEALING and value >= 500)
		doPlayerSendTextMessage(cid, 22, "Woah, you've just healed for more than 500 hit points!")
	end
end
onAttack(cid, target) - every time when player attacking something.
onKill(cid, target) - every time when player killing something(target).
onDeath(cid, corpse, lastHitKiller, mostDamageKiller) - every time when player(or creature) dies.
onPrepareDeath(cid, lastHitKiller, mostDamageKiller) - this event going to be executed before player(or creature) dies, so, script can be executed before player(creature) dies. For example if you will add to player +1 hit point with onPrepareDeath event, players(or creatures) will be immortal.

Also, in next release of TFS 0.3 there will be such creature events like onCast, onCombat and onAreaCombat.


Here you can found lua functions list!

Here I will explain a small example script(it's party hat's script):

PHP:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	if item.uid == getPlayerSlotItem(cid, CONST_SLOT_HEAD).uid then
		doSendMagicEffect(getPlayerPosition(cid), CONST_ME_GIFT_WRAPS)
		return TRUE
	end
	return FALSE
end

function onUse(cid, item, fromPosition, itemEx, toPosition) - as you can see, this script going to be executed when player used the item.
getPlayerSlotItem(cid, slot) - This function will check what item player have actually in slot. This function returns array with item which is actually in slot. When slot is empty, then return = 0 (FALSE).
if item.uid == getPlayerSlotItem(cid, CONST_SLOT_HEAD).uid then - Now, we checking if the item which player used, is in players head slot. item.uid - item which player used. .uid means unique id of item. Every item got his own unique id, so it's not possible to have 2 SAME items, with same unique ids.
doSendMagicEffect(getPlayerPosition(cid), CONST_ME_GIFT_WRAPS) - This function(doSendMagicEffect(pos, type[, creature])) sends a magic effect to specific potision. getPlayerPosition(cid) returns array with players position. CONST_ME_GIFT_WRAPS type of effect, you can found all effect types in your lib\constant.lua file.
return TRUE - A return statement returns occasional results from a function or simply finishes a function. There is an implicit return at the end of any function, so you do not need to use one if your function ends naturally, without returning any value. So, when you return TRUE, you returning, what script was succes.


Here I will try to explain a more difficult script(I used surprise bag script):

PHP:
local bluePresent = {2687, 6394, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114}
local redPresent = {2152, 2152, 2152, 2153, 5944, 2112, 6568, 6566, 2492, 2520, 2195, 2114, 2114, 2114, 6394, 6394, 6576, 6576, 6578, 6578, 6574, 6574}

function onUse(cid, item, fromPosition, itemEx, toPosition)
	local count = 1
	if item.itemid == 6570 then
		local randomChance = math.random(1, 11)
		if randomChance == 1 then
			count = 10
		elseif randomChance == 2 then
			count = 3
		end
		doPlayerAddItem(cid, bluePresent[randomChance], count)
	elseif item.itemid == 6571 then
		local randomChance = math.random(1, 22)
		if randomChance > 0 and randomChance < 4 then
			count = 10
		end
		doPlayerAddItem(cid, redPresent[randomChance], count)
	end
	doSendMagicEffect(fromPosition, CONST_ME_GIFT_WRAPS)
	doRemoveItem(item.uid, 1)
	return TRUE
end

Lets start.

Code:
local bluePresent = {2687, 6394, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114}
local redPresent = {2152, 2152, 2152, 2153, 5944, 2112, 6568, 6566, 2492, 2520, 2195, 2114, 2114, 2114, 6394, 6394, 6576, 6576, 6578, 6578, 6574, 6574}
- we declaring 2 arrays(tables). First array called bluePresent and second called redPresent. In every array, we store some values, every value separated by comma.
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
- thats how we start every action script.(script executed when item was used)
Code:
local count = 1
- declaring local value called count, and storing value "1" there.
Code:
if item.itemid == 6570 then
- if item id(item "number") which we used equal to 6570, then we executing next part of script, if not then we going to execute "else" part of this "if" statement.
Code:
local randomChance = math.random(1, 11)
- declaring new storage value called randomChange, and storing there a random number between 1 and 11.
Code:
if randomChance == 1 then
			count = 10
		elseif randomChance == 2 then
			count = 3
		end
- checking our random number stored in value randomChance. If this value equal to 1 then we assign value "10" into storage called count. elseif randomChance == 2 then If, value randomChance is NOT 1, and equals to 2, then we assign value "3" into storage called count.
Code:
doPlayerAddItem(cid, bluePresent[randomChance], count)
- This function(doPlayerAddItem(uid, itemid[, count/subtype[, canDropOnMap]])) gives a specific item to player. So, in this part, we adding to player item, from array bluePresent. So, if, for example randomChance(which is random), equals to 4, then we adding to player item, equals to position 4 in array bluePresent(it's item id 6574).
Code:
doSendMagicEffect(fromPosition, CONST_ME_GIFT_WRAPS)
- I already explained how this function works.
Code:
doRemoveItem(item.uid, 1)
- This function (doRemoveItem(uid[, n])) removes an item. In this part we removing item which we used, so it makes our surprise bag dissapears. 1 means, the count of items, which we need to be removed.

what if function not =1 or =2 (the random function) what will it do u said its random between 1~11? so if its not 1 neither 2 , so if its 3 or 4..5.6.7.8.9.10.. or 11? what will happen?
 
In this tutorial I will generally explain how to work with action scripts.
  1. Useful information (read when you are new with this)
  2. Lua functions

----1 Useful information

When editing the .lua / .xml files the best editor to use is simply Notepad. Also it's very easy to use Notepad ++, scripting with this program is very easy, and comfortable. You can download notepad++ HERE

---1.1 Storing data

When scripting in ot action files you will have to understand what some thing mean. One of the important things are items in which you can store data. Here I will give an example:


Code:
number = 1
Now the storage item "number" contains "1". So each time we will call up the storage value "number", we will get the answer "1".

When you'd like to perform math calculations you can do it like this:

Code:
number = number + 1
Now there will be added 1 to the storage value number. You can replace + with -, / and some more math signs.
If you want number to be an random number it would be like this:

Code:
number=math.random(1,10)

1,10 is the min/max number that the random init should get.(math.random is a function)

It is also possible to store text inside these storage items. An example:

Code:
name = "Hi, my name is Fare"
Now the storage item "name" will contain the text "Hi, my name is Fare".

I highly recommend to use local in scripts. What they are supposed to? When announcing a value, (anything = 5), you creating a storage value which store "5", after script was executed, you don't need to keep this "5", becouse script was already executed. Thats why I recommend to use local values. The word ‘local’ means that the variable is only used only when executing script. As soon as the script is done, the memory is cleared again.

---1.2 "If" statements:
An if statement tests its condition and executes its then-part or its else-part accordingly. The else-part is optional.
Another words, "if" statement simply says that a piece of code between the "if" and the "end" will only be carried out when the lines of code between the "if" and the "then" is correct.
You always need to close an "if" statement with and "end".

Example:
Code:
if apple == 1 then
"piece of code"
end

This code will carry out the "piece of code" only if the storage value "apple" is 1. Look at the "==" part. When using an if statement and you want to compare two factors you must use the "=" signe twice instead of once!
If you want the "if" statement to compare multiple factors in one statement it will look like this:
Code:
if apple == 1 and pear == 2 then
"piece of code"
end

Now the "piece of code" will only be carried out when the storage value "apple" is 1 and "pear" is 2. Of course you can put as many factors in one if statement, as long as you separate them with an "and".
If you'd like to carry out "piece of code" when the storage value "apple" is 1 OR when the storage value "pear" is 2 simply replace the "and" by an "or".

Let's say you want to execute "piece of code1" when the storage value "apple" is 1, and "piece of code2" when the storage value "apple" is not 1. It will have to look like this:

Code:
if apple == 1 then
"piece of code"
else
"piece of code2"
end

You simply put an "else" between the "if ... then" and the "end". Everything which is between the "if .. then" and the "else" will be carried out if the storage value apple IS 1, everything which is between the "else" and "end" will be carried out when and store value apple is NOT 1.


Now, what if you want "piece of code1" to be carried out when the storage value "apple" is 1, and "piece of code2" when the storage value "apple" is 2. When apple is neither 1 nor 2 “piece of code3” will be carried out. Example:

Code:
if apple == 1 then
"piece of code1"
elseif apple == 2 then
"piece of code2"
else
"piece of code3"
end


If the storage value "apple" is 1 "piece of code1" will be carried out, but if the storage value "apple" is 2 ONLY "piece of code2" will be carried out!
When the apple is neither 1 nor 2 "piece of code 3" will be carried out.
You can put as many "elseif...then"'s between the "if...then" and the "else" as you want.
If you want nothing to happen when the apple is neither 1 or 2 simple remove the(else "piece of code3")


When you are using "==" between apple and 1(if apple == 1 then) you are comparing. So you say, if the storage value of "apple" is 1 then ....
When you use the "==" sign, the part in front of the "==" (apple) most of the time represents a storage item.
But it would be nice if we could do other things then comparing factors! Therefore we can place a few different things between the storage value and then thing you want to compare (or something else) it to.

Examples:

-
Code:
if apple > 1 then
(will only be carried out if the storage value "apple" is bigger then 1)
-
Code:
if apple < 1 then
(will only be carried out if the storage value "apple" is smaller then 1)
-
Code:
if apple ~= 1 then
(will only be carried out if the storage value "apple" is not 1)
-
Code:
if apple >= then
(will only be carried out if the storage value "apple" is 1, or bigger then 1)
-
Code:
if apple <= then
(will only be carried out if the storage value "apple" is 1, or smaller then 1)


---1.3 "For" statements

For statements are completly different then if statements. Here is an example:

Code:
for apple 1,10 do
doPlayerSendTextMessage(cid,18,apple)
end

Now, I will explain a bit.
"doPlayerSendTextMessage(cid,18,apple)" is an function (more about this later) which will send a message to the player with a text. Now the text which will be send is the contains of the storage item "apple". In this code the for statement will create a loop. It will repeat "doPlayerSendTextMessage(cid,18,apple)" until the storage value apple is 10.

Code:
for apple [B]nr1[/B],[B]nr2[/B] do
Here nr1 is the starting number, nr2 is the finishing number. The for loop will automatically increase the value of the storage item "apple" each time it repeats the loop.


---1.4 Repeat statements

Repeat statements will repeat a piece of code until something reaches a value. Example:


Code:
Repeat
“piece of code”
Until NumberOfApples > NumberOfPearsNow

the “piece of code” wil be repeated untill the storage value NumberOfApples is bigger then the storage value NumberOfPears. Ofcourse you can change NumberOfPears into a number.




Credits goes to Mindstorm!
I just found this tutorial and I think it can be usefull, so I updated, completed, and posted it here.

----2 Commands(lua functions)
When scripting action files there are a lot of lua functions which are useful to know. Here I will explain the once which you will use most.

function onUse(cid, item, fromPosition, itemEx, toPosition)”:
This is what every action script starts with. The function onUse() just means that you are using something (a item or whatever). There are a few "storage values" you are given in the action script, which you don’t have to set! These storage values are:
-cid: The person who uses the item (info about the person using).
-item: Info on the item he is using. (so if you ctrl-click on a blue berry bush a lot information about that blue berry bush is stored inside the item storage value).
-fromPosition: From wich position character is going to use the item.
-itemEx: if character is using the item on something, itemEx contains data of that item.
-toPosition: to which position character is using to the item.
Keep in mind that fuction onUse() always ends with an “end”

There are also some other events(based on TFS 0.3. Creaturescripts:
onLogin(cid) - this script executing when every player has loggin in.
onLogout(cid) - when every players logging out.
onJoinChannel(cid, channel, users) - when player joining any speak channel(trade,help, ect)
onLeaveChannel(cid, channel, users) - when player leaving speak channel.
onAdvance(cid, skill, oldLevel, newLevel) - when player get skill advance. If can be level, magic level, sword fighting, fishing, anything.
onSendMail(cid, receiver, item, openBox) - when player send mail ^^
onReceiveMail(cid, sender, item, openBox) - when player receive mail.
onLook(cid, position) - when player looks on anything(shift + click, or right + left mouse click)
onThink(cid, interval) - this type of event executing every interval time(used mostly in globalevents, with specific interval.
onStatsChange(cid, attacker, type, combat, value) - when players stats going to be changed(hp, mana, ect). Example:
Code:
 onStatsChange(cid, attacker, type, combat, value)
	if(type == STATSCHANGE_HEALTHGAIN and combat == COMBAT_HEALING and value >= 500)
		doPlayerSendTextMessage(cid, 22, "Woah, you've just healed for more than 500 hit points!")
	end
end
onAttack(cid, target) - every time when player attacking something.
onKill(cid, target) - every time when player killing something(target).
onDeath(cid, corpse, lastHitKiller, mostDamageKiller) - every time when player(or creature) dies.
onPrepareDeath(cid, lastHitKiller, mostDamageKiller) - this event going to be executed before player(or creature) dies, so, script can be executed before player(creature) dies. For example if you will add to player +1 hit point with onPrepareDeath event, players(or creatures) will be immortal.

Also, in next release of TFS 0.3 there will be such creature events like onCast, onCombat and onAreaCombat.


Here you can found lua functions list!

Here I will explain a small example script(it's party hat's script):

PHP:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	if item.uid == getPlayerSlotItem(cid, CONST_SLOT_HEAD).uid then
		doSendMagicEffect(getPlayerPosition(cid), CONST_ME_GIFT_WRAPS)
		return TRUE
	end
	return FALSE
end

function onUse(cid, item, fromPosition, itemEx, toPosition) - as you can see, this script going to be executed when player used the item.
getPlayerSlotItem(cid, slot) - This function will check what item player have actually in slot. This function returns array with item which is actually in slot. When slot is empty, then return = 0 (FALSE).
if item.uid == getPlayerSlotItem(cid, CONST_SLOT_HEAD).uid then - Now, we checking if the item which player used, is in players head slot. item.uid - item which player used. .uid means unique id of item. Every item got his own unique id, so it's not possible to have 2 SAME items, with same unique ids.
doSendMagicEffect(getPlayerPosition(cid), CONST_ME_GIFT_WRAPS) - This function(doSendMagicEffect(pos, type[, creature])) sends a magic effect to specific potision. getPlayerPosition(cid) returns array with players position. CONST_ME_GIFT_WRAPS type of effect, you can found all effect types in your lib\constant.lua file.
return TRUE - A return statement returns occasional results from a function or simply finishes a function. There is an implicit return at the end of any function, so you do not need to use one if your function ends naturally, without returning any value. So, when you return TRUE, you returning, what script was succes.


Here I will try to explain a more difficult script(I used surprise bag script):

PHP:
local bluePresent = {2687, 6394, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114}
local redPresent = {2152, 2152, 2152, 2153, 5944, 2112, 6568, 6566, 2492, 2520, 2195, 2114, 2114, 2114, 6394, 6394, 6576, 6576, 6578, 6578, 6574, 6574}

function onUse(cid, item, fromPosition, itemEx, toPosition)
	local count = 1
	if item.itemid == 6570 then
		local randomChance = math.random(1, 11)
		if randomChance == 1 then
			count = 10
		elseif randomChance == 2 then
			count = 3
		end
		doPlayerAddItem(cid, bluePresent[randomChance], count)
	elseif item.itemid == 6571 then
		local randomChance = math.random(1, 22)
		if randomChance > 0 and randomChance < 4 then
			count = 10
		end
		doPlayerAddItem(cid, redPresent[randomChance], count)
	end
	doSendMagicEffect(fromPosition, CONST_ME_GIFT_WRAPS)
	doRemoveItem(item.uid, 1)
	return TRUE
end

Lets start.

Code:
local bluePresent = {2687, 6394, 6280, 6574, 6578, 6575, 6577, 6569, 6576, 6572, 2114}
local redPresent = {2152, 2152, 2152, 2153, 5944, 2112, 6568, 6566, 2492, 2520, 2195, 2114, 2114, 2114, 6394, 6394, 6576, 6576, 6578, 6578, 6574, 6574}
- we declaring 2 arrays(tables). First array called bluePresent and second called redPresent. In every array, we store some values, every value separated by comma.
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
- thats how we start every action script.(script executed when item was used)
Code:
local count = 1
- declaring local value called count, and storing value "1" there.
Code:
if item.itemid == 6570 then
- if item id(item "number") which we used equal to 6570, then we executing next part of script, if not then we going to execute "else" part of this "if" statement.
Code:
local randomChance = math.random(1, 11)
- declaring new storage value called randomChange, and storing there a random number between 1 and 11.
Code:
if randomChance == 1 then
			count = 10
		elseif randomChance == 2 then
			count = 3
		end
- checking our random number stored in value randomChance. If this value equal to 1 then we assign value "10" into storage called count. elseif randomChance == 2 then If, value randomChance is NOT 1, and equals to 2, then we assign value "3" into storage called count.
Code:
doPlayerAddItem(cid, bluePresent[randomChance], count)
- This function(doPlayerAddItem(uid, itemid[, count/subtype[, canDropOnMap]])) gives a specific item to player. So, in this part, we adding to player item, from array bluePresent. So, if, for example randomChance(which is random), equals to 4, then we adding to player item, equals to position 4 in array bluePresent(it's item id 6574).
Code:
doSendMagicEffect(fromPosition, CONST_ME_GIFT_WRAPS)
- I already explained how this function works.
Code:
doRemoveItem(item.uid, 1)
- This function (doRemoveItem(uid[, n])) removes an item. In this part we removing item which we used, so it makes our surprise bag dissapears. 1 means, the count of items, which we need to be removed.

an tip is that you use a
] --> (php] MSG (/php]
 
Nice one :) Will help me for sure when I start scripting (in a week or so when I get home from my dad)

Rep++ ofc :thumbsup:
 
Back
Top