
Forum




Pick a player at random
28 replies
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
nemesis = 0 function randomplayer() 	local tt = player(0,"team1") 	local ct = player(0,"team2") 	local id = math.rand(1,#tt + #ct) 	return tt[id] or ct[id] end addhook("startround","_startround") function _startround() 	math.randomseed(os.time()) --seed every once in a while 	nemesis = randomplayer() --get the new 'nemesis' 	parse("equip "..nemesis.." 45") --give the player a laser end
edited 1×, last 22.05.11 10:43:20 pm
1
2
3
4
5
2
3
4
5
math.randomseed(os.clock()) function randomplayer() 	local t = player(0,"table") 	return t[math.rand(1,#t)] end

Picking random players is good for things like changing the player into a nemesis (i will need on a future script, im certain of it).
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
math.randomseed(os.clock()) function randomplayer() local t = player(0,"table") local id=t[math.rand(1,#t)] if (player(id,"team")~=0) then return id else randomplayer() end end
1
2
3
4
5
6
7
2
3
4
5
6
7
function randomplayer() 	local t = player(0,"table") 	if #t > 0 then 		return t[math.rand(1,#t)] 	end 	return nil end
And returns nil if nobody is in the game.
edited 1×, last 23.05.11 12:35:38 am

how would I make it only choose non-spec players

If the player count is 0, then you can't select someone random into 1 and 0
1
2
3
4
2
3
4
function randomplayer() 	local t = player(0, "tableliving") 	return #t > 0 and t[math.random(1,#t)] or 0 end

1
2
2
local rand_id = randomplayer() parse("equip "..rand_id.." 45")

correction:
1
2
3
4
5
2
3
4
5
math.randomseed(os.clock()) function randomplayer() 	local t = player(0,"table") 	return t[math.rand(1,#t)] end
os.clock() as randomseed initiator seems not to work well for me. I got the same person with second hook like 7 times in a row. Any other good initiator?

You should use os.time() not os.clock()
IT's better to use os.clock().
You can use os.time() too, but that would be senseless unless you use it at exactly the same time very day.
Which ID did the player it picked have?

os.clock() as randomseed initiator seems not to work well for me. I got the same person with second hook like 7 times in a row. Any other good initiator?
you can't really avoid that by using another seed. things like that can always happen - because it's (pseudo) random. you might also get a 6 X times in a row when you throw a dice often enough.
what you could do is saving the last X results of that function in variable and let it select another player if it chooses one of the x saved results again.


You should use os.time() not os.clock()
IT's better to use os.clock().
You can use os.time() too, but that would be senseless unless you use it at exactly the same time very day.
Which ID did the player it picked have?
No, os.clock returns the number of milliseconds that the script has ran (If you started the script 67 milliseconds ago, os.clock() will return the number 67) whereas os.time returns the number of seconds passed since 1970. Under ideal conditions, os.clock should theoretically return a very limited range of seeds whereas os.time will return better seeds. However, it may be advisable to mask all but the lowest 32 bytes of os.time.