Forum

> > CS2D > Scripts > Pick a player at random
Forums overviewCS2D overview Scripts overviewLog in to reply

English Pick a player at random

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

old Re: Pick a player at random

DanielG
User Off Offline

Quote
Um, I believe this should be my last question, and I'll just say what I'm trying to do.

How would I randomly pick one alive person on a server, and place on the Terrorist team, and place all the others on the Counter Terrorist Team.

Again, thanks

old Re: Pick a player at random

Starkkz
Moderator 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
function RandomPlayer()
	local t = {}
	for _, id in pairs(player(0,"table")) do
		if player(id,"team") > 0 then
			table.insert(t,id)
		end
	end
	if #t > 0 then
		return t[math.random(1,#t)]
	end
	return nil
end

function SetTeams()
	local p = RandomPlayer()
	if p == nil then return nil end
	if player(p,"team") ~= 1 then parse("maket "..p) end
	for _, id in pairs(player(0,"table")) do
		if player(id,"team") > 0 and id ~= p then
			parse("makect "..id)
		end
	end
end

use the functions SetTeams to change the players teams.

old Re: Pick a player at random

DanielG
User Off Offline

Quote
I'm getting the same problems I was getting when I tried to do it myself, it sort of works, but everytime it switches team, everyone switches, and is dead, so the round restarts, and everyone switches again, and is dead and so the round starts again, and etc.

old Re: Pick a player at random

Starkkz
Moderator Off Offline

Quote
It depends on wich hook are you using it, you may show your lua script to see what mistake did you make.

old Re: Pick a player at random

Starkkz
Moderator Off Offline

Quote
That's the error. At the startround, when there are 1 or 2 players in the server, the round automatically restarts, and the hook will be called again, and again, and again... So, i recomend:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
countDown = 10
addhook("startround","onStart")
function onStart()
	countDown = 10
end

addhook("second","onSec")
function onSec()
	if countDown > 0 then
		countDown = countDown - 1
		msg(countDown.."@C")
	elseif countDown == 0 then
		countDown = -1
		SetTeams()
	end
end

10 seconds after the round start, a random player will be selected to be the terrorist.

old Re: Pick a player at random

DanielG
User Off Offline

Quote
Now, every ten secs, everyone dies and one person is switched to Terrorist. Everyone that isn't the chosen person dies and is sent to CT team. Sorta like before, everyone dies and the teams are split as intended.
I'm trying to use this script on standard mode by the way.

old Re: Pick a player at random

Starkkz
Moderator Off Offline

Quote
Hah! there's it. I forgot the respawning thing, i'll fix that.

Edit: Ok, now try it

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
function RandomPlayer()
	local t = {}
	for _, id in pairs(player(0,"table")) do
		if player(id,"team") > 0 then
			table.insert(t,id)
		end
	end
	if #t > 0 then
		return t[math.random(1,#t)]
	end
	return nil
end

function SetTeams()
	local p = RandomPlayer()
	if p == nil then return nil end
	if player(p,"team") ~= 1 then
		local TEnts = {}
		for x = 0, map("xsize")+1 do
			for y = 0, map("ysize")+1 do
				if entity(x,y,"exists") then
					if string.lower(entity(x,y,"typename") )== "info_t" then
						table.insert(TEnts,{x = x,y = y})
					end
				end
			end
		end
		parse("maket "..p)
		if #TEnts > 0 then
			local randEnt = TEnts[math.random(1,#TEnts)]
			parse("spawnplayer "..p.." "..((randEnt.x*32)+16).." "..(randEnt.y*32)+16)
		end
	end
	
	local ctEnts = {}
	for x = 0, map("xsize")+1 do
		for y = 0, map("ysize")+1 do
			if entity(x,y,"exists") then
				if string.lower(entity(x,y,"typename") )== "info_ct" then
					table.insert(ctEnts,{x = x,y = y})
				end
			end
		end
	end
	for _, id in pairs(player(0,"table")) do
		if player(id,"team") > 0 and id ~= p and player(id,"team") ~= 2 then
			parse("makect "..id)
			if #ctEnts > 0 then
				local randEnt = ctEnts[math.random(1,#ctEnts)]
				parse("spawnplayer "..id.." "..((randEnt.x*32)+16).." "..(randEnt.y*32)+16)
			end
		end
	end
end

countDown = 10
addhook("startround","onStart")
function onStart()
	countDown = 10
end

addhook("second","onSec")
function onSec()
	if countDown > 0 then
		countDown = countDown - 1
		msg(countDown.."@C")
	elseif countDown == 0 then
		countDown = -1
		SetTeams()
	end
end

If doesn't work, then it means that it will not work for standard mode.
edited 1×, last 23.05.11 10:51:26 pm
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview