Forum

> > CS2D > Scripts > finding a special player id in timer function...
Forums overviewCS2D overview Scripts overviewLog in to reply

English finding a special player id in timer function...

5 replies
To the start Previous 1 Next To the start

old finding a special player id in timer function...

khaled1968
User Off Offline

Quote
I wanted to create a bot that automatically talk; so i writed this code...

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
teamcolor = {}

addhook("spawn" ,"playerspawn")
function playerspawn(id)
if player(id,"team") == 1 then
teamcolor[id] = "\169255025000"
else
teamcolor[id] = "\169050150255"
end
end

timer(math.random(3000,5000),"naheztalking","",0)
function naheztalking()

     local playerlist = player(0,"tableliving")
     for _,id in pairs(playerlist) do
	 
     local nahezspeaks = math.random(1,7)

if nahezspeaks == 1 then
msg(""..teamcolor[id].."nahez: hey")
return 1
else
if nahezspeaks == 2 then
msg(""..teamcolor[id].."nahez: hello")
return 1
else
if nahezspeaks == 3 then
msg(""..teamcolor[id].."nahez: test")
return 1
else
if nahezspeaks == 4 then
msg(""..teamcolor[id].."nahez: hhhh")
return 1
else
if nahezspeaks == 5 then
msg(""..teamcolor[id].."nahez: asfaf")
return 1
else
if nahezspeaks == 6 then
msg(""..teamcolor[id].."nahez: gfdgfd")
return 1
else
if nahezspeaks == 7 then
msg(""..teamcolor[id].."nahez: eyffdsa")
return 1
end
end
end
end
end
end
end
end
end

the only problem im facing is the "id" thing in the timer! the bot is speaking in different "teamcolor" than the team he really is in...

Edit: sorry if i had some stupid scripting mistakes im still new to it, and sorry for my bad english.

old Re: finding a special player id in timer function...

Cebra
User Off Offline

Quote
you have to find the id(s) of your bot(s) and save them, with your approach you will get <number of living player>-times a message, all in different colours (depending on the team of the players).

furthermore i cleaned your code a bit.

The code is untested!

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
botIDs = {}
teamcolor = {}

addhook("spawn" ,"playerspawn")
function playerspawn(id)
	if player(id,"bot") == true then
		table.insert(botIDs, id)
		if player(id,"team") == 1 then
			table.insert(teamcolor, "\169255025000")
		else
			table.insert(teamcolor, "\169050150255")
		end
	end
end

timer(math.random(3000,5000),"naheztalking","",0)

function naheztalking()
	for index,id in ipairs(botIDs) do
		if player(id,"health") > 0 then
			local nahezspeaks = math.random(1,7)
			if nahezspeaks == 1 then
				msg(""..teamcolor[index].."nahez: hey")
			elseif nahezspeaks == 2 then
				msg(""..teamcolor[index].."nahez: hello")
			elseif nahezspeaks == 3 then
				msg(""..teamcolor[index].."nahez: test")
			elseif nahezspeaks == 4 then
				msg(""..teamcolor[index].."nahez: hhhh")
			elseif nahezspeaks == 5 then
				msg(""..teamcolor[index].."nahez: asfaf")
			elseif nahezspeaks == 6 then
				msg(""..teamcolor[index].."nahez: gfdgfd")
			elseif nahezspeaks == 7 then
				msg(""..teamcolor[index].."nahez: eyffdsa")
			end
		end
	end
end

edit: maybe you take a look into the awesome tutorial by user Starkkz: thread cs2d [GUIDE] How to script
edited 1×, last 02.08.22 12:20:36 pm

old Re: finding a special player id in timer function...

Cure Pikachu
User Off Offline

Quote
@user Cebra: If we are talking "player" bots, honestly it'll be better to try and incorporate cs2d lua cmd ai_say into it, if it works outside the confines of AI scripting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bottext = {
	"hey",
	"hello",
	"test",
	"hhhh",
	"asfaf",
	"gfdgfd",
	"eyffdsa"
}

timer(math.random(3000,5000),"naheztalking","",0)

function naheztalking()
	for index, id in pairs(player(0,"tableliving")) do
		if player(id,"bot") then
			ai_say(id,bottext[math.random(1,#bottext)])
		end
	end
end
Might need to find a way to not make them all say their lines simutaneously though.
edited 1×, last 09.11.22 03:00:06 pm

old Re: finding a special player id in timer function...

khaled1968
User Off Offline

Quote
user Cure Pikachu has written
@user Cebra: If we are talking "player" bots, honestly it'll be better to try and incorporate cs2d lua cmd ai_say into it, if it works outside the confines of AI scripting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bottext = {
	"hey",
	"hello",
	"test",
	"hhhh",
	"asfaf",
	"gfdgfd",
	"eyffdsa"
}

timer(math.random(3000,5000),"naheztalking","",0)

function naheztalking()
	for index, id in pairs(player(0,"playerliving")) do
		if player(id,"bot") then
			ai_say(id,bottext[math.random(1,#bottext)])
		end
	end
end
Might need to find a way to not make them all say their lines simutaneously though.


LUA ERROR: sys/lua/autorun/test.lua:14: bad argument #1 to 'pairs' (table expected, got boolean)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview