Jump to content

Element tables and math.random


DRW

Recommended Posts

Hello, I'm currently working on a gamemode, but I have a problem, because I need to script something that sets an elementdata to some of the currently online players. Take the Gmod's gamemode "Trouble in Terrorist Town" for example, that gamemode gets all the online and alive players and sets the "traitor" data to a minority portion of the current online players. I know I should use something like getElementsByType or getPlayerCount, and probably math.random, but I know it won't work how it has to work, because it may select randomly two times the same player, and I don't want that. Any help?

Link to comment
  
local players = getElementsByType ( "player" ) -- get all players connected 
  
local thirdpart = math.floor(#players / 3) -- get 1/3 count of those players 
  
-- you could use many other loop method, I'd probably use this one though 
local i = 1 
  
while i  <= thirdpart do 
     local value = math.random(0, #players ) -- generates a random value between "0" and the amount of players 
                 if isElement(players[value]) then 
                                  setElementData(players[value], string, data) 
                                  i = i + 1  
                 end 
end 
  
  

EDIT: Edited my post, totally forgot you wanted it to be random.

Link to comment

or actually, totally forgot about a function

https://wiki.multitheftauto.com/wiki/GetRandomPlayer

  
local players = getElementsByType ( "player" ) -- get all players connected 
local thirdpart = math.floor(#players / 3) -- get 1/3 count of those players 
  
local i = 1 
while i  <= thirdpart do 
       local random = getRandomPlayer() 
       setElementData(random, string, data) 
       i = i + 1 
end 
  

Edited by Guest
Link to comment
or actually, totally forgot about a function

https://wiki.multitheftauto.com/wiki/GetRandomPlayer

  
local players = getElementsByType ( "player" ) -- get all players connected 
local thirdpart = math.floor(#players / 3) -- get 1/3 count of those players 
  
local i = 1 
while i  <= thirdpart do 
       local random = getRandomPlayer() 
       setElementData(random, string, data) 
end 
  

Thank you very much!

Link to comment

what about.. note: i am posting from my phone so i can't enter wiki and get correct functions

function Random () 
local Player = getRandomPlayer() 
local Count = getPlayerCount() 
if Count == 0 then return end 
 if Player then 
    if getElementData ( Player, key ) == value then return end 
    setElementData ( Player, key , value ) 
end 
end 
setTimer(Random, your_time_in_secs*1000, 0) 
      

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...