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? Proud owner and developer of ZNEXT: Aftermath. Enter a post-apocalyptic San Andreas and fight over 30 types of enemies and bosses with varying difficulties and skills, improve and customize your character by leveling up, completing challenges and a solid lootbox system with no Pay-to-Win mechanics that will break your experience. Meet new characters, creatures and weapon metas, experience an innovative combo-based melee system, or join our solid PvP modes to show other survivors who’s boss. Español, Pусский, Türk, عربى, Polski, Português IP: mtasa://104.36.110.227:22003 - Discord: https://discord.gg/CxMxjvC5pB
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. Proud owner and developer of ZNEXT: Aftermath. Enter a post-apocalyptic San Andreas and fight over 30 types of enemies and bosses with varying difficulties and skills, improve and customize your character by leveling up, completing challenges and a solid lootbox system with no Pay-to-Win mechanics that will break your experience. Meet new characters, creatures and weapon metas, experience an innovative combo-based melee system, or join our solid PvP modes to show other survivors who’s boss. Español, Pусский, Türk, عربى, Polski, Português IP: mtasa://104.36.110.227:22003 - Discord: https://discord.gg/CxMxjvC5pB
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... Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
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! Proud owner and developer of ZNEXT: Aftermath. Enter a post-apocalyptic San Andreas and fight over 30 types of enemies and bosses with varying difficulties and skills, improve and customize your character by leveling up, completing challenges and a solid lootbox system with no Pay-to-Win mechanics that will break your experience. Meet new characters, creatures and weapon metas, experience an innovative combo-based melee system, or join our solid PvP modes to show other survivors who’s boss. Español, Pусский, Türk, عربى, Polski, Português IP: mtasa://104.36.110.227:22003 - Discord: https://discord.gg/CxMxjvC5pB
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) You can find me here.
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