Forum

> > CS2D > Scripts > Delete string...?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Delete string...?

6 replies
To the start Previous 1 Next To the start

old Delete string...?

KenVo
User Off Offline

Quote
Hey guys, I don't know how to delete string after I use string.find.
Example:
A player join my server and his name is "[Admin] Player". I found "admin" in his name, but I don't know how to delete "admin". I checked the lua documentary, but I can never understand for some reasons...

old Re: Delete string...?

Apache uwu
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
addhook("join","_join")
addhook("name","_name")

unacceptable={"admin","vip","etc"}

function _join(id)
	if stripWords(player(id,"name"),unacceptable)~=string.lower(player(id,"name")) then
		parse("setname "..id.." "..stripWords(player(id,"name"),unacceptable))
	end
end

function _name(id,oldname,newname)
	if stripWords(newname,unacceptable)~=string.lower(newname) then
		return 1
	end
end

function stripWords(start,words)
	for index,value in ipairs(words) do
		words[index]=string.lower(value)
	end
	start=string.lower(start)
	for _,key in ipairs(words) do
		while string.find(start,key)~=nil do
			start=string.gsub(start,key,"")
		end
	end
	return start
end

Like this?

old Re: Delete string...?

KenVo
User Off Offline

Quote
Lol thank you! I didn't expect the whole script. Let me sit my ass down and try to understand.

EDIT: So I tried to explain that what each line does just to make sure I got it. Please fix me if I'm wrong.
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
--Player's name is [Admin]KenVo

addhook("join","_join") --Add hook on joining
addhook("name","_name") --Add hook on changing name

unacceptable={"admin","vip","etc"} --Words that are not allow in player's name

function _join(id) --On join
	if stripWords(player(id,"name"),unacceptable)~=string.lower(player(id,"name")) then --if []KenVo(stripped admin from [Admin]KenVo) is NOT [Admin]KenVo
		parse("setname "..id.." "..stripWords(player(id,"name"),unacceptable))--then set player's name to the stripped name
	end
end

function _name(id,oldname,newname) --On changing name in game
	if stripWords(newname,unacceptable)~=string.lower(newname) then --if the new name that the player is changing to has matched unacceptable letters ([]KenVo to [Admin]KenVo)
		return 1 --then the player's name can't get changed
	end
end

function stripWords(start,words)
	for index,value in ipairs(words) do --loop the unacceptable's table
		words[index]=string.lower(value) --change all the upper case letters in the table to lower case letters
	end
	start=string.lower(start) --change all the upper case letters of 1st parameter to lower case letters
	for _,key in ipairs(words) do --loop the unacceptable's table
		while string.find(start,key)~=nil do --make sure if the letters of 1st parameter match the letters of the table
			start=string.gsub(start,key,"") --take out the matched letters from the 1st parameter
		end
	end
	return start --idk how to explain this...???
end
edited 2×, last 28.01.12 06:30:21 am

old Re: Delete string...?

Kel9290
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
32
33
34
35
36
37
38
39
40
41
addhook("join","_join")
addhook("name","_name")

adminlist={"14706"}
unacceptable={"admin","vip","etc"}

function isadmin(i)
	for u=1,#adminlist do
		if adminlist[u] == player(i,"usgn") then
			return true
		end
	end
	return false
end

function _join(id)
	if isadmin(id) == false then
		if stripWords(player(id,"name"),unacceptable)~=string.lower(player(id,"name")) then
			parse("setname "..id.." "..stripWords(player(id,"name"),unacceptable))
		end
	end
end

function _name(id,oldname,newname)
	if stripWords(newname,unacceptable)~=string.lower(newname) then
		return 1
	end
end

function stripWords(start,words)
	for index,value in ipairs(words) do
		words[index]=string.lower(value)
	end
	start=string.lower(start)
	for _,key in ipairs(words) do
		while string.find(start,key)~=nil do
			start=string.gsub(start,key,"")
		end
	end
	return start
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview