DRW Posted August 4, 2015 Posted August 4, 2015 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?
DRW Posted August 4, 2015 Author Posted August 4, 2015 setElementData I was clearly not asking for setElementData, just asking how to use math.random to set an elementData to one third part of the total online players.
anumaz Posted August 4, 2015 Posted August 4, 2015 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.
anumaz Posted August 4, 2015 Posted August 4, 2015 (edited) 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 August 4, 2015 by Guest
Moderators IIYAMA Posted August 4, 2015 Moderators Posted August 4, 2015 tweak When there are 0 players the while loop will still be executed. A simple > if #thirdpart > 0 then < on line 4 would do the fix. Also count i = i+1 some where else...
DRW Posted August 4, 2015 Author Posted August 4, 2015 or actually, totally forgot about a functionhttps://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!
MAB Posted August 5, 2015 Posted August 5, 2015 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now