Forum

> > CS2D > Scripts > Lua Tutorial by FN_Linkin Park
Forums overviewCS2D overview Scripts overviewLog in to reply

English Lua Tutorial by FN_Linkin Park

27 replies
Page
To the start Previous 1 2 Next To the start

moved Lua Tutorial by FN_Linkin Park

J4x
User Off Offline

Quote
Hi everyone. I decided to Share my knowledge a about lua in cs2d, so i make this tutorial.

First we will learn what is a hook. A hook is not a real Lua part is just a code that a game or application can understand. In cs2d we have different hooks, here are some examples :
> second: This hook is trigered every second.
> Spawn: This hook is triggered everytime a player spawns
> Start round: This hook is trigered every time that round starts.

Everytime we will use a hook we have to add the following Things: the word addhook and 2 ". So if i like to use the second hook i have to write this:
1
addhook("second","lol")
So now u should be asking urself, whe did i write "lol"? I write "lol" because we have to add a name to our hook, so the name that i add to my hook is lol, u can add every name that u want. so lest say that i want to name my hook FN, then i will only change the "lol" by a FN. Example:
1
addhook("second","FN")

----------------------------
Now i will explain u the functions. A function is always written under the hook, and the name of the hook is used as the function, in this case "lol" will be our function. So now we will write our hook + our function :
1
2
addhook("second","lol")
function lol()
Now u should be asking why i add ()?
the () are parts of the function, inside the () we add the function parameters, this parameters are default ones, u will find them inside the info.txt archive inside the lua folder. Now lets make our first script In our first script we will use the start round hook,so here we go! Lets say that u want to make a script that everytime that a round starts it sends u a message. We will start our code with the hook and the function:
1
2
addhook("startround","lol") <---Start round is the Hook and lol the hook's name.
function lol()<---- Some hooks didn't have parameters, in this case the startround hook didn't has a paremeter.
But how i will do that the server send me a message? with this
1
- msg("text")
where sasy "text" u can write what ever u want.
So our script is
1
2
3
4
addhook("startround","lol")
funtion lol()
msg("Prepare to fight")
end
Why do add end to the script? because end indicates cs2d that our script has ended. Yay! we have our first script!
----------------------------
now lets do something more dificult. I want to make a script that makes that when i am terrorist it sends me a message.
Now the things will be diferent, we will have to use our [bTeam hook[/b] and the if statement. Why the if satement? because the script will work only if u are terorrist. So lets start:
1
2
3
4
5
6
addhook("team","wtf")
function wtf(id,team) <--- We can add more than 1 paremeter in the ()
if player (id,"team") == 1 then
the msg2(id,"You are terrorist!")
end
end
Why do i write if player (id,"team") == 1 then? because this is the part that will make our script work, only if the player team is 1 (1 = terrorist,2=counter terrorist) will work. but now i dont like that my script works only if i am a terorist, i like it work if i am conuter terrorist. Well we just replace the 1 in if player (id,"team") == 1 by a 2 so it will be if player (id,"team") == 2.
---------------------
Now lets go with the errors.
Normally if a script dont works, a message in red will appear in the console [to open the console pers the ~ key or just write /console].
NOte: Cs2d has to be open to open the console...

Common erros are this ones:
<eof> expected near <end>
end expected to close at line 49 near <eof>
But wtf is <eof>? eof means end of file.

How do we solve this problems?
the first one is saying us that there is a unnecessary end in our file(a extra end), what do we do? we remove a end. example
1
2
3
4
5
addhook("startround","lol")
funtion lol()
msg("Prepare to fight")
end
end <----- this is end is unnecessary, so we have to remove it

The second one is the contrary of the first one.
it says us that our script lacks a end, so we have to add another end to our script. example:
1
2
3
4
5
6
addhook("team","wtf")
function wtf(id,team) 
if player (id,"team") == 1 then
the msg2(id,"You are terrorist!")
end
      <--- here we should add a end to solve the problem.


Note : if u dont understand something write in the coments ;D.

Common question:
1.I dont found the info.txt! dont worry here it is, just save it in ur desktop.
http://www.mediafire.com/?m6efd522u8hpovo


2.What u teach me here is everything about lua?
Of course not, lua is a complex language, and it would be to hard to long, to teach it all to you, but u cand find a few tutorials in google.

3. Only cs2d uses lua?
no, a lot of engines, aplications and games use lua, note that each game and application uses lua in a different way.
edited 17×, last 04.04.11 12:30:38 am

Admin/mod comment

please use the scripts forum for everything about Lua

old Re: Lua Tutorial by FN_Linkin Park

HaRe
User Off Offline

Quote
1
2
if player (id,"team") == 1
the msg("You are terrorist!")

the???, Dont you mean

1
2
if player (id,"team") == 1 then
msg("You are terrorist!")

also

1
function wtf(id,team] <--- We can add more than 1 paremeter in the ()

even tought its

1
function wtf(id,team) <--- We can add more than 1 paremeter in the ()


But well its an good tut, will help many newbies

old Re: Lua Tutorial by FN_Linkin Park

NKN
User Off Offline

Quote
please show me how to do this, if ct moves on certain tile, show a message for ct
by example, if ct moves on tiles (x,y) 5,25 or 6,25 or 7,25
show a message "Its Locked" for cts only once

old Re: Lua Tutorial by FN_Linkin Park

J4x
User Off Offline

Quote
Sorry for thos who like a video, i cant make one. @Necronash
u have to use the move hook to do that
1
2
3
4
5
6
7
8
9
addhook("move","ct")
function ct(id,y,x)
if player(id,"team") == 2 then
if player(id,"x") == 5 then
if player(id,"y") == 25 then
msg("Its Locked")
end
end
end

old Re: Lua Tutorial by FN_Linkin Park

Vectarrio
User Off Offline

Quote
@FN_Linkin Park, NKN
lol, this is not like that
I think it must be movetile hook:
1
2
3
4
5
6
addhook("movetile","ctm")
function ctm(id,x,y)
	if player(id,"team")==2 and x==5 and y==25 then
		msg2(id,"it's locked")
	end
end

old Re: Lua Tutorial by FN_Linkin Park

Slayerwllpku
User Off Offline

Quote
Made a simple script it doesn't work help T-T
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
------------
Made by Slayerwllpku
Help Command don't steal
------------
addhook("say", "help")
function help (id,txt)
if(txt==!help") then
msg("Welcome! "..id" This is only for admins:")
msg("==========Admin Commands==========")
msg("!a - teleport forward")
msg("!b - broadcast to server with name")
msg("!c - teleport player to you")
msg("!d - broadcast to server without name")
msg("!e - EXPLODE!!!!")
msg("!i - Spawn Item")
msg("!h - heal player")
msg("!k - kick Player (Put there I.D)
msg("!l run lua script (expensive)
msg("!n - return npc position")
msg("!o - return tile position")
msg("!p - return position")
msg("!q - eartquake")
msg("!s - speedmod")
msg("!t - teleport to player
msg("!u - shutdown (NEVER DO THIS UNTIL THE APPROVAL OF THE OWNER")
msg("!v - save server")
msg("!x - ban player")
msg("=================Made By Slayerwllpku===========")
end
end
end

old Re: Lua Tutorial by FN_Linkin Park

Vectarrio
User Off Offline

Quote
Slayerwllpku has written
Made a simple script it doesn't work help T-T
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
------------
Made by Slayerwllpku
Help Command don't steal
------------
addhook("say", "help")
function help (id,txt)
if(txt==!help") then
msg("Welcome! "..id" This is only for admins:")
msg("==========Admin Commands==========")
msg("!a - teleport forward")
msg("!b - broadcast to server with name")
msg("!c - teleport player to you")
msg("!d - broadcast to server without name")
msg("!e - EXPLODE!!!!")
msg("!i - Spawn Item")
msg("!h - heal player")
msg("!k - kick Player (Put there I.D)
msg("!l run lua script (expensive)
msg("!n - return npc position")
msg("!o - return tile position")
msg("!p - return position")
msg("!q - eartquake")
msg("!s - speedmod")
msg("!t - teleport to player
msg("!u - shutdown (NEVER DO THIS UNTIL THE APPROVAL OF THE OWNER")
msg("!v - save server")
msg("!x - ban player")
msg("=================Made By Slayerwllpku===========")
end
end
end

msg("!k - kick Player (Put there I.D)
you confused brackets and forgot " thing.
so, it must be:
msg("!k - kick Player (Put there I.D)")
and about shutdown - bracket missing too (but that's about text, so, it's not a bug)
msg("!u - shutdown (NEVER DO THIS UNTIL THE APPROVAL OF THE OWNER)")
anyway, I recommend command msg2 instead of just msg.
msg2(id,"text")

also bracket missing:
msg("!l run lua script (expensive)
must be
msg("!l run lua script (expensive)")
,
msg("!t - teleport to player
must be
msg("!t - teleport to player")

old Re: Lua Tutorial by FN_Linkin Park

Slayerwllpku
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
------------
Made by Slayerwllpku
Help Command don't steal
------------
addhook("say", "help")
function help (id,txt)
if(txt==!help") then
msg2(id,"Admin Commands")
msg("==========Admin Commands==========")
msg("!a - teleport forward")
msg("!b - broadcast to server with name")
msg("!c - teleport player to you")
msg("!d - broadcast to server without name")
msg("!e - EXPLODE!!!!")
msg("!i - Spawn Item")
msg("!h - heal player")
msg("!k - kick Player (Put there I.D)")
msg("!l run lua script (expensive)")
msg("!n - return npc position")
msg("!o - return tile position")
msg("!p - return position")
msg("!q - eartquake")
msg("!s - speedmod")
msg("!t - teleport to player")
msg("!u - shutdown (NEVER DO THIS UNTIL THE APPROVAL OF THE OWNER")
msg("!v - save server")
msg("!x - ban player")
msg("=================Made By Slayerwllpku===========")
end
end
end
Like this cause it says Attempt to a call strings

old Re: Lua Tutorial by FN_Linkin Park

Vectarrio
User Off Offline

Quote
------------
Made by Slayerwllpku
Help Command don't steal
------------

change it to:

------------
--Made by Slayerwllpku
--Help Command don't steal
------------

also
(txt==!help")
again:
(txt=="!help")

please, check all script for these mistakes!
edited 1×, last 09.02.11 07:38:32 pm

old Re: Lua Tutorial by FN_Linkin Park

GreenDevil
User Off Offline

Quote
EngiN33R has written
I don't understand why do you guys want a video? What will be in that video? Someone writing scripts in notepad?


At first i think this will help much i though i will learn more about lua than reading... But no...

Video is useless

old Re: Lua Tutorial by FN_Linkin Park

The Camo
User Off Offline

Quote
Slayerwllpku has written
Like this cause it says Attempt to a call strings


Its better to place "Help" or some long stuff like that one with msg2, since players can spam it otherwise. here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
--------------------------
--Script by Slayerwllpku--
-------Do not steal!------
--------------------------
addhook("say", "sayhelp")
function sayhelp(id,txt)
if(txt=="!help") then
msg2(id,"Admin Commands")
msg2(id,"==========Admin Commands==========")
msg2(id,"!a - Teleport forward")
msg2(id,"!b - Broadcast to server with name")
msg2(id,"!c - Teleport player to you")
msg2(id,"!d - Broadcast to server without name")
msg2(id,"!e - EXPLODE!!!!")
msg2(id,"!i - Spawn item")
msg2(id,"!h - Heal player")
msg2(id,"!k - Kick Player (Input ID)")
msg2(id,"!l - Run lua script (expensive)")
msg2(id,"!n - Return npc position")
msg2(id,"!o - Return tile position")
msg2(id,"!p - Return position")
msg2(id,"!q - Eartquake")
msg2(id,"!s - Speedmod")
msg2(id,"!t - Teleport to player")
msg2(id,"!u - Shutdown (NEVER DO THIS UNTIL THE APPROVAL OF THE OWNER")
msg2(id,"!v - Save server")
msg2(id,"!x - Ban player")
msg2(id,"==========Script by Slayerwllpku=========="); return 1
end;end

old Re: Lua Tutorial by FN_Linkin Park

plaYnice
User Off Offline

Quote
Thanks for this tutorial
I loved it. It does more in Prison for different scripts.

explain to me what command to @cageall? (Cage means putting walls around the character.
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview