Forum

> > CS2D > Scripts > Random Selecting Players
Forums overviewCS2D overview Scripts overviewLog in to reply

English Random Selecting Players

2 replies
To the start Previous 1 Next To the start

old Random Selecting Players

Waldin
User Off Offline

Quote
Hello im making a lua script and i need your help:
I want in a list of alive players, a random number 1-3, only X with 1, only X with 2 and only X with 3 (and dont repeat everytime the seed). then in a table, the key with the ID of player and the value is the number.
example
12 players, i want 4 with M3, 2 with Deagle and 6 with shield and machete.
I cant find a way, if you can help me please do! thanks you.

sry for typos and bad grammar.
edited 1×, last 23.05.16 03:21:16 am

old Re: Random Selecting Players

VADemon
User Off Offline

Quote
Here's the approach:
12 players, you want 4/2/6 to have Shotgun/Deagle/Melee Kit. In numbers that is:
• 4/12 = 0.333 = 33.3% of players
• 2/12 = 0.166 = 16.6%
• 6/12 = 0.5     = 50%

First calculate how many people should have the different kits (sort of pseudocode):
1
2
3
4
5
6
7
8
shotgunPlayers = math.floor(playersLiving * 0.333)
deaglePlayers = math.floor(playersLiving * 0.166)
meleePlayers = math.floor(playersLiving * 0.5)

-- fill one group in case of bad rounding
if (shotgunPlayers + deaglePlayers + meleePlayers) < playersLiving then
	meleePlayers = meleePlayers + 1 
end

Now that we have the exact numbers, we'll equip the players (actual 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
-- team 1 or 2 living
local teamliving = player(0,"team1living")

-- execute as often as many shotgun kits will be given
for i = 1, shotgunPlayers do
	local randomkey = math.random(1, #teamliving)	-- random key inside that table
	local pid = teamliving[ randomkey ]	-- e.g. teamliving[3], 3rd player in this table
	local wid = 88	-- weapon id
	
	parse("equip ".. pid .." ".. wid)
	
	table.remove(teamliving, randomkey)	-- remove this player from table
end

-- same for deagle
for i = 1, deaglePlayers do
	local randomkey = math.random(1, #teamliving)	-- random key inside that table
	local pid = teamliving[ randomkey ]	-- e.g. teamliving[3], 3rd player in this table
	local wid = 3	-- weapon id
	
	parse("equip ".. pid .." ".. wid)
	
	table.remove(teamliving, randomkey)	-- remove this player from table
end

-- same for armor + machete, add armor yourself
for i = 1, meleePlayers do
	local randomkey = math.random(1, #teamliving)	-- random key inside that table
	local pid = teamliving[ randomkey ]	-- e.g. teamliving[3], 3rd player in this table
	local wid = 69	-- weapon id
	
	parse("equip ".. pid .." ".. wid)
	
	table.remove(teamliving, randomkey)	-- remove this player from table
end
With this code you're guaranteed to equip a certain amount of people with different kits. Each player is randomly picked.

PS: you need to glue the first and the second part of the code together
PPS: Fix the title.
PPPS: To others: user Waldin took at least some time to write a proper, understandable request. I would not answer otherwise. Follow his example.
edited 2×, last 23.05.16 02:46:19 am

old Thanks VADemon

Waldin
User Off Offline

Quote
Thanks you! i'll use and test it.
PS: Kits just example, but i can use your code to make my script
edit: alot of errors LOL, already solved.
edit2x: I have other problem... (solved xd)
edited 4×, last 23.05.16 04:13:06 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview