Hero192 Posted February 27, 2016 Share Posted February 27, 2016 Hello guys, I tried to make a random jobs shows in the GUI and be taken, but when I use math.random function it return just one result, so If I had 10Peds, it will returns with 10 Jobs with the same name not different, and I want it to be different. I tried that but failed, local jobtypes = {"Criminals", "Air Force", "Military", "Police"} local peds = { [createPed(0,2616.33, -1298.55, 14.23, 120)] = {0, 0}, [createPed(0,725.57, -1240.45, 13.53, 90)] = {0, 0}, } for ped, v in pairs(peds) do if ped then setElementData(ped,"jobPed",true) setElementData(ped,"jobnames",jobtypes[math.random(#jobtypes)]) setElementFrozen(ped, true) end end I hope you guys give a hand, and thanks Link to comment
Hero192 Posted February 27, 2016 Author Share Posted February 27, 2016 Anyone please? Link to comment
Noki Posted February 27, 2016 Share Posted February 27, 2016 math.random(1, #jobtypes) Link to comment
Hero192 Posted February 27, 2016 Author Share Posted February 27, 2016 math.random(1, #jobtypes) It returns all strings who exist on the table , anyway? It's possible alot of servers do that, I want for each Ped get a random job name Link to comment
Noki Posted February 27, 2016 Share Posted February 27, 2016 It doesn't return all strings in the table. math.random takes two values, a minimum value and a maximum value. It returns one value, a random number. You're getting a value in a table from that number as an index. Link to comment
Tox Posted February 27, 2016 Share Posted February 27, 2016 you can either set an id variable out of the chunk that increments when peds get jobs like --outside chunk local id = 0 --inside chunk id = id +1 if id >= #jobtypes then id = 1 end or you can repeat math.random until it gives another result and it's like this; --outside chunk local oldID = 0 --inside chunk local id = 1 repeat id = math.random (#jobtypes) until id ~= oldID oldID = id Link to comment
Hero192 Posted February 27, 2016 Author Share Posted February 27, 2016 Hey Tox and thanks for the reply, How can I use it for example in my code? Link to comment
Tox Posted February 27, 2016 Share Posted February 27, 2016 Hey Tox and thanks for the reply, How can I use it for example in my code? depends on the which one you want to use but i'll just leave the both here; --1st way-- local jobtypes = {"Criminals", "Air Force", "Military", "Police"} local id = 0 local peds = { [createPed(0,2616.33, -1298.55, 14.23, 120)] = {0, 0}, [createPed(0,725.57, -1240.45, 13.53, 90)] = {0, 0}, } for ped, v in pairs(peds) do if ped then id = id + 1 if id >= #jobtypes then id = 1 end setElementData(ped,"jobPed",true) setElementData(ped,"jobnames",jobtypes[id]) setElementFrozen(ped, true) end end --2nd way-- local jobtypes = {"Criminals", "Air Force", "Military", "Police"} local int, old = 1, 1 local peds = { [createPed(0,2616.33, -1298.55, 14.23, 120)] = {0, 0}, [createPed(0,725.57, -1240.45, 13.53, 90)] = {0, 0}, } for ped, v in pairs(peds) do if ped then repeat int = math.random(#jobtypes) until int ~= old old = int setElementData(ped,"jobPed",true) setElementData(ped,"jobnames",jobtypes[int]) setElementFrozen(ped, true) end end Link to comment
Hero192 Posted February 28, 2016 Author Share Posted February 28, 2016 Doesn't work, still return with the same result, that has returned from math.random to all peds. For more explanation, I want for each ped get his random job, so like that not all peds would be with the same job. Hope you can give me a hand Link to comment
Saml1er Posted February 28, 2016 Share Posted February 28, 2016 Your first code is right. Just add this before loop. math.randomseed (getTickCount () ) -- Now you can loop through table and use math.random Link to comment
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