Jump to content

Random job for each ped


Hero192

Recommended Posts

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

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
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

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...