Forum

> > CS2D > Scripts > Lua Scripts/Questions/Help
Forums overviewCS2D overview Scripts overviewLog in to reply

English Lua Scripts/Questions/Help

6,770 replies
Page
To the start Previous 1 2 3338 339 Next To the start

old Re: Lua Scripts/Questions/Help

DC
Admin Off Offline

Quote
lol. the solution is right in your code...
you had one right if - end - construct there which is:
1
2
3
if (txt=="!deagle") then
	parse("equip "..id.." 3")
end

thinking should help.
edited 2×, last 18.04.09 01:47:30 am

old Re: Lua Scripts/Questions/Help

Flacko
User Off Offline

Quote
You could do it either this way:
1
2
3
4
5
6
7
8
9
10
11
12
addhook("say","player_say")
function player_say(id,txt)
	if(txt=="!usp") then
		parse("equip "..id.." 1")

	elseif(txt=="!glock") then
		parse("equip "..id.." 2")

	elseif(txt=="!deagle") then
		parse("equip "..id.." 3")
	end
end

or this one, which is like the one you wrote, but with the extra "end" erased

1
2
3
4
5
6
7
8
9
10
11
addhook("say","player_say")
function player_say(id,txt)
	if(txt=="!usp") then
		parse("equip "..id.." 1") end

	if(txt=="!glock") then
		parse("equip "..id.." 2") end

	if(txt=="!deagle") then
		parse("equip "..id.." 3") end
end

I would recommend you the first way, IMO, it's more memory efficient

old Re: Lua Scripts/Questions/Help

Lee
Moderator Off Offline

Quote
also, if you download the weaponids package by Kiffer-Opa, you can also do this
http://www.unrealsoftware.de/files_show.php?file=1016
1
2
3
4
5
6
7
8
9
10
11
dofile("sys/lua/wpnids.lua")
addhook("say", "player_say")
function player_say(p, t)
	local delim = "!"
	if string.sub(t, 1. #delim) == delim then
		local cmd = string.sub(t, #delim)
		if type(_G[cmd]) == "number" then
			parse(string.format("equip %s %s", p, _G[cmd]))
		end
	end
end

This will basically give you the weapon that you need if you just type in !weapon-name

for example:

!USP
!Glock
!Deagle
!HE
...

and everything else that's in the wpnids.lua, but they have to be case sensitive.

Elaborate on Flacko's point: The reason that first way is actually a lot better to do if you have a huge conditional list is because the elseif gate only traverses until it finds a true statement and then breaks whereas a string of if gates will only break after every single condition is checked.

Which also brings us to something else. It's better if you set a prefix for the commands because it's much more efficient, everytime that someone types something, that you only check if the first letter of the text is the same as the command prefix so you don't have to do a lot of false conditions when people just chat normally (as they are more proned to do then typing in commands) In this case, there would only need to be one condition to see whether the text you typed in is a command or not. This may not seem significant until you began to have hundreds of commands built in (The most I've worked with have closed to around 90+ commands). In this case you will notice a significant lag when talking if you just throw the whole command in since it's checking 90 conditions each time (they would all return false so it goes through every single one of them) instead of just 1 and then << or = 90.

old Re: Lua Scripts/Questions/Help

Lee
Moderator Off Offline

Quote
Flacko has written
Does someone know about a good syntax highlighter for lua? I'm damn tired of that crappy wordpad


Try Scite

old weps

vrkiller
User Off Offline

Quote
A script where you can get all wepeons when you chat

!all_wep


1
2
3
4
5
6
7
8
9
10
11
12
13
addhook("say","player_say") 
function player_say(id,txt) 
    
          if(txt=="!all_wep") then 
          lol=0
          while(lol<=100) do
             parse("equip "..id.." "..lol) 
             parse("say you just got all weps") 
             parse("sv_sound \"Soundfile\"")
           lol=lol+1

   
 end end  end

old Re: Lua Scripts/Questions/Help

DC
Admin Off Offline

Quote
wow your code formatting is pure brainfuck

that's how you format a code:
1
2
3
4
5
6
7
8
9
10
11
12
addhook("say","player_say")
function player_say(id,txt)
	if(txt=="!all_wep") then
		lol=0
		while(lol<=100) do
			parse("equip "..id.." "..lol)
			parse("say you just got all weps")
			parse("sv_sound \"Soundfile\"")
			lol=lol+1
		end
	end
end
this makes reading and understanding much easier.
moreover some things you should change:

• use if (itemtype(lol,"name")~="") then to check if this item exists (=has a name) before you try to assign it.

• careful! remove the say and sound commands from the loop! otherwise they will be executed 101 times! this is a very critical error which also causes massive traffic (it could also kill the server in worst case)! execute them before or after the loop!

• use for lol = 1,100 do instead of the while loop. so you don't need the lol=lol+1!

• I recommend to use msg instead of say.

old Re: Lua Scripts/Questions/Help

Flacko
User Off Offline

Quote
This is a script blackdeath requested me to do for his map, the new thing respect all the other classes scripts, is that some classes can drop special items (with the drop key (G, for those smartasses))...

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
if flacko==nil then flacko={} end
flacko.heroes={}

-------------------
--FUNCTIONS--
-------------------
function initArray(m,F)
	local array = {}
	for i = 1, m do
		array[i]=F
	end
	return array
end

function flacko.heroes.setclass(id,hp,armor,speed)
	parse("setmaxhealth "..id.." "..hp)
	parse("setarmor "..id.." "..armor)
	parse("speedmod "..id.." "..speed)
end

function hudtext2(id,tid,color,txt,x,y)
	toprint = ("©"..color.." "..txt)
	parse('hudtxt2 '..id..' '..tid..' "'..toprint..'" '..x.." "..y)
	--PARAMETERS: 1-Player ID| 2-Text ID| 3-COLOR| 4-TEXT| 5/6-POSITION|
end

function flacko.heroes.classmenu(id)
	if(flacko.heroes.commanders==0) then
		menu(id,"Select your Class,War Machine,Ninja,Calibur,Bomber,Sniper,Engineer,Commander")
	elseif(flacko.heroes.commanders==1) then
		menu(id,"Select your Class,War Machine,Ninja,Calibur,Bomber,Sniper,Engineer")
	end
end
--------------------------------------
--Some variables and tables--
--declarations over here      --
--------------------------------------
flacko.heroes.class=initArray(32,0)
flacko.heroes.specitemtimer = 0
flacko.heroes.specitems=initArray(32,0)
flacko.heroes.limit = 15
flacko.heroes.commanders = 0


parse("mp_randomspawn 1")
parse("sv_gamemode 4")

------------------------
-- TEAM -> CLASS--
------------------------
addhook("team","flacko.heroes.team")
function flacko.heroes.team(id,team)
	if (team==2) then
		flacko.heroes.classmenu(id)
	end
end


-----------------------
--SERVERACTION--
-----------------------
addhook("serveraction","flacko.heroes.serveraction")
function flacko.heroes.serveraction(id)
	flacko.heroes.classmenu(id)
end
--I don't know what is this for, but i left it from the original classes code

---------------------------
--CLASS SELECTION--
---------------------------
addhook("menu","flacko.heroes.menu")
function flacko.heroes.menu(id,menu,sel)
	if (menu=="Select your Class") then
		if (sel>=0 and sel<=7) then
			flacko.heroes.class[id]=sel
			if (player(id,"health")>0) then
				parse("killplayer "..id)
			end
		end
	end
end


-----------------------
-- SPAWN           --
-----------------------
addhook("spawn","flacko.heroes.spawn")
function flacko.heroes.spawn(id)

	local weaponstring
	
	hudtext2(id,1,"","",0,0) --Clears HUD texts

	if(player(id,"team")==2) then
		
		if(flacko.heroes.class[id]==0) then
			flacko.heroes.class[id]=math.random(1,6)
		end
		
		if(flacko.heroes.class[id]==1) then
			flacko.heroes.setclass(id,120,203,0)
			weaponstring = "40,48,76"

		elseif(flacko.heroes.class[id]==2) then
			flacko.heroes.setclass(id,85,206,35)
			weaponstring = "69"
			hudtext2(id,1,"000255000","Special items: "..flacko.heroes.specitems[id],250,420)
			--Ninjas drop mines

		elseif(flacko.heroes.class[id]==3) then
			flacko.heroes.setclass(id,90,201,0)
			weaponstring = "30,10"
			hudtext2(id,1,"000255000","Special items: "..flacko.heroes.specitems[id],250,420)
			--Calibur drops primmary ammo

		elseif(flacko.heroes.class[id]==4) then
			flacko.heroes.setclass(id,60,0,5)
			weaponstring = "49,51,76,72"

		elseif(flacko.heroes.class[id]==5) then
			flacko.heroes.setclass(id,85,204,-5)
			weaponstring = "35,53,59"
			hudtext2(id,1,"000255000","Special items: "..flacko.heroes.specitems[id],250,420)
			--Sniper drops medikits

		elseif(flacko.heroes.class[id]==6) then
			flacko.heroes.setclass(id,100,204,7)
			weaponstring = "11,74"

		elseif(flacko.heroes.class[id]==7) then
			flacko.heroes.setclass(id,150,203,10)
			flacko.heroes.commanders = 1
			weaponstring = "32,11,36,69"
		end
	
	end
	return weaponstring;
end


-----------------------
-- NO BUYING     --
-----------------------
addhook("buy","flacko.heroes.buy")
function flacko.heroes.buy()
	return 1
end


-------------------------
--NO COLLECTING--
-------------------------
addhook("walkover","flacko.heroes.walkover")
function flacko.heroes.walkover(id,iid,type)
	if (type>=61 and type<=68) or (type==77) or (type>=70 and type<=71) then
		return 0
	end
	return 1
end


-----------------------
-- NO DROPPING--
-----------------------
addhook("drop","flacko.heroes.drop")
function flacko.heroes.drop(id)
	if(player(id,"team")==2) then
		if(flacko.heroes.specitems[id]>=1) then
			if(flacko.heroes.class[id]==5) then --Snipers drop Medikits
				flacko.heroes.specitems[id] = flacko.heroes.specitems[id]-1
				hudtext2(id,1,"000255000","Special items: "..flacko.heroes.specitems[id],250,420)
				parse("spawnitem 64 "..player(id,"tilex").." "..player(id,"tiley"))

			elseif(flacko.heroes.class[id]==3) then --Caliburs drop primmary ammo
				flacko.heroes.specitems[id] = flacko.heroes.specitems[id]-1
				parse("spawnitem 61 "..player(id,"tilex").." "..player(id,"tiley"))
				hudtext2(id,1,"000255000","Special items: "..flacko.heroes.specitems[id],250,420)

			elseif(flacko.heroes.class[id]==2) then --Ninjas drop mines
				flacko.heroes.specitems[id] = flacko.heroes.specitems[id]-1
				parse("spawnitem 77 "..player(id,"tilex").." "..player(id,"tiley"))
				hudtext2(id,1,"000255000","Special items: "..flacko.heroes.specitems[id],250,420)
			end
		end
	end
	return 1
end


------------------------------
--NO DEAD DROPPING--
------------------------------
addhook("die","flacko.heroes.die")
function flacko.heroes.die(id)
	if(flacko.heroes.class[id]==7) then
		flacko.heroes.commanders = 0
		flacko.heroes.class[id] = math.random(1,6)
	end
	return 1
end

-------------------------------
--SPECIAL ITEM TIMER --
-------------------------------
addhook("second","flacko.heroes.second")
function flacko.heroes.second()
	flacko.heroes.specitemtimer = flacko.heroes.specitemtimer+1

	if(flacko.heroes.specitemtimer>=flacko.heroes.limit) then
		flacko.heroes.specitemtimer = 0
		for id=1, 32 do
			if(player(id,"exists")) then
				if(flacko.heroes.class[id]==5 or flacko.heroes.class[id]==3 or flacko.heroes.class[id]==2) then
					flacko.heroes.specitems[id]=flacko.heroes.specitems[id]+1
					hudtext2(id,1,"000255000","Special items: "..flacko.heroes.specitems[id],250,420)
				end
			end
		end
	end
end

-----------------------
--   ON JOINING  --
-----------------------
addhook("join","flacko.heroes.join")
function flacko.heroes.join(id)
	flacko.heroes.specitems[id]=0
	flacko.heroes.class[id]=0
end

old Re: Lua Scripts/Questions/Help

DC
Admin Off Offline

Quote
serveraction is a hook for the server action keys (f2, f3 and f4 by default)

you can bind actions to these keys by using the serveraction hook. in this case you can open the class selection menu by pressing f2, f3 or f4

old Args.lua

Lee
Moderator Off Offline

Quote
args.lua

I guess I owe this an explanation. Basically, this takes a command, either console or from say, as a text, and returns a table with each element.


1. args("guns 1 AK47")

returns

(Table){"guns", "1", "AK47"}



1. args('guns "some one" AK47')

returns

(Table){"guns", "some one", "AK47"}


now this is where it gets interesting. Not only can you just use it as a simple split function, you can also pass in a second argument that's either a String, a Number, or a Table.

Example 1: args* = a Number

1. args("sayto Player Hello Player", 3)

returns
(Table){"sayto", "Player", "Hello Player"}

This reverses the effects of normal spliting which truncates the 4th word and on.

Example 2: args* = a Table

1. args("sayto Player Hello Player", {"cmd", "id", "txt"})

returns
(Table){[cmd] = "sayto", [id] = "Player", [txt] = "Hello Player"}

Example 3: args* = a Table And an element is called *_id
Assume - Player is the player at ID = 3

1. args("sayto Player Hello Player", {"cmd", "player_id", "txt"})

returns
(Table){[cmd] = "sayto", [player] = 3, [txt] = "Hello Player"}

Example 4: args* = a String

1. args("sayto Player Hello Player", "cmd, id, txt")

returns
(Table){[cmd] = "sayto", [id] = "Player", [txt] = "Hello Player"}

Example 5: args* = a String And an element is called *_id
Assume - Player is the player at ID = 3

1. args("sayto Player Hello Player", "cmd, player_id, txt")

returns
(Table){[cmd] = "sayto", [player] = 3, [txt] = "Hello Player"}

Download:

http://amx2d.co.cc/download/file.php?id=7
args() - v1 - Nonextendable

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
--AMX2D Already has this built in.
if amx2d then return true end

function string.trim(t)
	t = string.split(t)
	s = ""
	if not t then return end
	for i, v in ipairs(t) do
		s = s.." "..v
	end
	s = string.sub(s, 2)
	return s
end

function string.split(t, b)
	local cmd = {}
	local match = "[^%s]+"
	if b then
		match = "%w+"
	end
	if type(b) == "string" then match = "[^"..b.."]+" end
	if not t then return nil end
	for word in string.gmatch(t, match) do
		table.insert(cmd, word)
	end
	return cmd
end

function string.inside(o, t)
	for i, v in ipairs(t) do
		if o==v then return true end
	end
end

function string.qsplit(t, d)
	if not d then d = '"' end
	local tab = string.split(t, d)
	if #tab == 1 then
		return string.split(tab[1])
	end
	local ret = {}
	for i, v in ipairs(tab) do
		if i%2 ~= 0 then
			table.insert(ret, v)
		else
			v = string.split(v)
			for _i, _v in ipairs(v) do
				table.insert(ret, _v)
			end
		end
	end
	return ret
end

function table.trim(t)
	local tab = {}
	for k, v in pairs(t) do
		if type(k) == "string" then k = k:trim() end
		if type(v) == "string" then v = v:trim() end
		tab[k] = v
	end
	return tab
end

function table.toStr(t, n, d)
	local s = ""

	if (n == nil) then n = 1 end
	if n<1 then n = 1 end
	if not d then d = " " end
	while (n <= #t) do
		s = s .. t[n] .. "".. d
		n = n + 1
	end
	return s

end

function isplayer(p)
	p = playerid(p)
	if not (type(p) == "number") then return false end
	if not player(p, "exists") then return false end
	return true
end

function playerid(i)
	i = string.trim(i)
	if tonumber(i) then
		if player(tonumber(i), "exists") then return tonumber(i) end
	end
	i = name2id(i)
	return i
end

function name2id(name)
	local names = {}
     for i =1, 32, 1 do
          if player(i, "exists") then
               names[player(i, "name")] = i
			end
     end
     if names[name] then return names[name] else return nil end
end

function id(name)
	return name2id(name)
end

function id2name(id)
	if player(id, "exists") then
		return player(id, "name")
	end
end

function name(id)
	return id2name(id)
end

if not player then
	function player(p, n)
		return n
	end
end
function args(t, n)
	if #t < 1 then return end
	if not n then n = #t:split() end
	local arg = {}
	if type(n) == "table" then
		arg = n
		n = #n
	elseif type(n) == "string" then
		arg = table.trim(string.split(n, ","))
		n = #arg
	end

	local t = t:split()
	local _t = {}
	if #t < n then n = #t end
	for i = 1, n-1 do
		local x = t[i]
		if #arg > 0 then
			if arg[i]:split("_") then
				if arg[i]:split("_")[2] == "id" then
					x = playerid(x)
					arg[i] = arg[i]:split("_")[1]
				end
			end
		end
		if not x then x = t[i] end
		if tonumber(x) then x = tonumber(x) end
		x = string.trim(x)
		if #arg > 0 then
			_t[arg[i]] = x

		end

		table.insert(_t, string.trim(t[i]))
	end

	local last = table.toStr(t, n)
	if #last == 0 then last = nil end
	local _l = last
	if #arg > 0 then
		if arg[n]:split("_") then
			if arg[n]:split("_")[1] == "id" then l = playerid(last) end
		end
	end
	if l then last = l end
	if tonumber(last) then last = tonumber(last) end
	last = string.trim(last)
	if #arg > 0 then
		_t[arg[n]] = last
	end

	table.insert(_t, string.trim(table.toStr(t, n)))
	return _t
end

t=args("guns name AK47", "cmd, player_id, gun")

print(t.cmd, t.player, t.gun)

old Re: Lua Scripts/Questions/Help

Carton
User Off Offline

Quote
Hi i have a question about lua script.
I openned a sample, advertise.lua and i don't know this code :
1
2
3
4
addhook("join","sample.ads.join")
function sample.ads.join(p,t)
	msg2(p,"Welcome on my Server, "..player(p,"name").."!")
end

Why are there "p" and "t" there ?
1
function sample.ads.join(p,t)
Is "p" = every player?

old Re: Lua Scripts/Questions/Help

ohaz
User Off Offline

Quote
these are parameters the function gives you. p is the player that joins. (why the hell is there a t?)
you could have named this parameter player, or any other word. it depends on the order in which the parameters are.

old Re: Lua Scripts/Questions/Help

Carton
User Off Offline

Quote
Ok thanks but if there are :
1
function lol(1,2,3)
can i do
1
function lol(1,3)
?
If i don't put the second parameter but only the first and third?

old Re: Lua Scripts/Questions/Help

Carton
User Off Offline

Quote
ok thanks.
Sorry again but i don't know this code in classes.lua
1
2
3
4
5
6
function initArray(m)
	local array = {}
	for i = 1, m do
		array[i]=0
	end
	return array
I know its a loop but i don't know why his utility?

old Re: Lua Scripts/Questions/Help

ohaz
User Off Offline

Quote
it creates an array with 'm' zeros.
so that you can create a 0 for every player on the server!
if now a player joins he has 0, you can now assign him another number. (like the class he choses)
To the start Previous 1 2 3338 339 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview