Forum

> > CS2D > Scripts > The Very Basic guide of CS2D Lua
Forums overviewCS2D overview Scripts overviewLog in to reply

English The Very Basic guide of CS2D Lua

9 replies
To the start Previous 1 Next To the start

old The Very Basic guide of CS2D Lua

sCy
User Off Offline

Quote
So, in this guide I teach you to make a basic script.

Items needed:
Time
Patience

So, we need to start with hook, like this:
1
addhook("spawn", "_spawn")

Now you have this one covered, we can do the function!

1
2
addhook("spawn", "_spawn")
function _spawn(id)

You may wonder why there is the ID. It is because player spawns, it takes his id what we need in script.

Also, we need to add end to the script.

Lets look of the script now:

1
2
3
addhook("spawn", "_spawn")
function _spawn(id)
end

So, this script doesnt do anything yet. But it could be made equip you weapons with parse command.
Parse allows you to enter the console command you
want to use like trigger, equip, setmaxhealth, speedmod.

1
2
3
4
addhook("spawn", "_spawn")
function _spawn(id)
	parse("equip "..id.." 30")
end

So, in spawn it equips the player with AK-47.
Thats it, basic and simple script..
But you may need some randomness to make it more fun, always spawning with AK-47 isn't that fun.
So, we use math.random to get the randomisation.

1
2
3
4
5
6
7
8
9
addhook("spawn", "_spawn")
function _spawn(id)
	local random = math.random(1, 2)
	if random == 1 then
		parse("equip "..id.." 30")
		elseif random == 2 then
		parse("equip "..id.." 32")
	end
end

Now you have the randomness between guns, so you can now possibly end with
an AK-47 or M4A1. You can also use the math.random in equip as well, like this:

1
2
3
4
addhook("spawn", "_spawn")
function _spawn(id) 
	parse("equip "..id.." "..math.random(1,89))
end

That should equip you with random item.

You may want that only CT get certain equips and Tr another. Then you can use this: player(id, "team")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
addhook("spawn", "_spawn")
function _spawn(id)
	local random = math.random(1, 2)
	if player(id, "team") == 1 then
		if random == 1 then
			parse("equip "..id.." 30")
		elseif random == 2 then
			parse("equip "..id.." 10")
		end
	elseif player(id, "team") == 2 then
		if random == 1 then
			parse("equip "..id.." 32")
		elseif random == 2 then
			parse("equip "..id.." 11")
		end
	end
end
See? Now if you are in TR team you have possibility to get M3 or AK-47, and if your in CT you have possibility
to get XM1014 or AK-47.

I hope you got the spark to start coding

Also, take a cookie for reading.

Edit: Changed name to be better, and more info.
edited 1×, last 15.02.12 07:11:31 pm

old Re: The Very Basic guide of CS2D Lua

DannyDeth
User Off Offline

Quote
"Those who know nothing, teach."

Never has this been more true. Don't try to teach people using a hook, this is NOT a part of Lua, but rather a part of CS2D's extra bits and pieces.

old Re: The Very Basic guide of CS2D Lua

Mechanolith
User Off Offline

Quote
user Time has written
Left 4 n00b has written
Items needed:
Time

damn

I see what you did there.

Well, if it's The very basic guide of lua it shouldn't be related to CS2D but to LUA itself, without hooks and stuff... am i right?

old Re: The Very Basic guide of CS2D Lua

EngiN33R
Moderator Off Offline

Quote
Indeed. This is a strictly CS2D beginner's guide, not a pure Lua one, which was, like many before it, written by a person who has just started himself.

It's arguably fine for the very beginning. Don't take me wrong, but as you probably understand, there are a lot more things to teach, and even this can be done more efficient with some more skill. Nevertheless, I like to see people having the will and initiative to help others, but in order to teach you need to learn much more yourself first.

old Re: The Very Basic guide of CS2D Lua

DarkLight66
User Off Offline

Quote
@user Mechanolith: Yes, as danny and ketamire said, this is just a basic guide of CS2D scripting, rater than lua scripting itself. Also, you are not explaining what math.random() or cs2d cmd parse does, even if their functions may be obvious, a guy with no knowledge about how cs2d or lua works may have a hard time understanding it.

old Re: The Very Basic guide of CS2D Lua

Flacko
User Off Offline

Quote
The Very Basic Guide of Lua - so basic it doesn't explain shit!

You're just copypasting code. A newbie wouldn't understand crap.

It's ok to show some code and tell what it does. But you should also explain why did you use it and how does it work.
edited 1×, last 15.02.12 07:03:26 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview