Page 1 of 1

random script

Posted: Tue Jun 02, 2009 11:07 am
by crazyfrag
is it possible to randomly choose a player that is on a server ? and how do i do this?

ty 4 ur help
regards crazy

Posted: Tue Jun 02, 2009 6:09 pm
by Father
If you need random choice you can use math.random().

Posted: Wed Jun 03, 2009 10:56 am
by crazyfrag
and how do i chooose the random player?

if i have privateslots (4 for example) how does he know from what number to what numer he is generating? if i have 4 guys on a server they may have slot numbers like 5 9 12 17 so if he chooses 10 theres no player...

i must return the mod if the slotnumber is not inuse and let him choose another number
how do i do this?

Posted: Wed Jun 03, 2009 12:00 pm
by McSteve
If not then (n+1)?

Posted: Wed Jun 03, 2009 12:13 pm
by crazyfrag
??

Posted: Wed Jun 03, 2009 12:30 pm
by crazyfrag
?? u mean for 16 slot server


function randomslot(slotinuse)
slots= tonumber(et.trap_Cvar_Get( sv_maxclients) - 1)

slotinuse = math.random(0,slots)
if not et.gentity_get(slotinuse,"inuse") then
slotinuse = slotinuse + 1

return slotinuse
end
end

Posted: Wed Jun 03, 2009 6:49 pm
by Father
The easiest way:

Code: Select all

.
.

local random_slot
repeat
  random_slot = math.random(0,slots)
until not et.gentity_get(random_slot,"inuse")

.
.

Better way:
  1. get numbers of slots in use
  2. put these numbers in table
  3. to get random:

    Code: Select all

    local random_slot = your_table[math.random(number_of_elements_in_table)]