Jump to content

Help me with the Random function


Hero192

Recommended Posts

Hello guys,

I want to make a random ped creation but, when I use what I had done with in below, each time I restart the resource for the random ped creation one ped of the other peds get combined with the other peds.

Here's what I had done with

local peds = { 
    [1] = {1067.0164794922, 2035.6273193359, 10.8203125, 90},   
    [2] = {1067.0299072266, 2041.1890869141, 10.8203125, 90},   
    [3] = {-2067.029296875, 2035.0201416016, 9.8203125,90},   
    [4] = {2067.0295410156, 1032.1505126953, 10.8203125,90},   
    [5] = {1067.0295410156, 2027.8729248047, 10.8203125,90},   
}  
   
function getRandomizePos() 
    local i = math.random(#peds) 
    local x,y,z,r = peds [i][1],peds [i][2],peds [i][3],depeds lerPeds[i][4] 
    return x,y,z,r 
end 
  
addEventHandler("onResourceStart", resourceRoot,  
function () 
    local x,y,z,rot = getRandomizePos() 
       peds =  createPed(0,x,y,z,rot) 
end) 

Special thanks to who gonna help

Link to post

try this:

local peds = { 
    [1] = {x=1067.0164794922,y=2035.6273193359, z=10.8203125, rot=90},   
    [2] = {x=1067.0299072266,y=2041.1890869141, z=10.8203125, rot=90},   
    [3] = {x=-2067.029296875,y=2035.0201416016, z=9.8203125,rot=90},   
    [4] = {x=2067.0295410156,y=1032.1505126953, z=10.8203125,rot=90},   
    [5] = {x=1067.0295410156,y=2027.8729248047, z=10.8203125,rot=90} -- the last dont have ,   
} 
  
function getRandomizePos() 
    local i = math.random(#peds) 
    local x,y,z,r = peds[i]["x"],peds[i]["y"],peds[i]["z"],peds[i]["rot"] 
    outputChatBox("index: "..i.." value x: "..x.." y: "..y.." z: "..z.." rot: "..rot) 
    return x,y,z,r 
end 
  
addEventHandler("onResourceStart", resourceRoot, 
function () 
    local x,y,z,rot = getRandomizePos() 
       peds =  createPed(0,x,y,z,rot) 
end) 

Edited by Guest
Link to post

try this no tested:

local repeteadIndex = {} 
  
local peds = { 
    [1] = {x=1067.0164794922,y=2035.6273193359, z=10.8203125, rot=90},   
    [2] = {x=1067.0299072266,y=2041.1890869141, z=10.8203125, rot=90},   
    [3] = {x=-2067.029296875,y=2035.0201416016, z=9.8203125,rot=90},   
    [4] = {x=2067.0295410156,y=1032.1505126953, z=10.8203125,rot=90},   
    [5] = {x=1067.0295410156,y=2027.8729248047, z=10.8203125,rot=90} -- the last dont have ,   
} 
  
function getRandomizePos() 
    local i 
    repeat i = math.random(#peds) 
    until(table.contains(repeteadIndex,i)==false) 
    table.insert(repeteadIndex,i) 
     
    local x,y,z,r = peds[i]["x"],peds[i]["y"],peds[i]["z"],peds[i]["rot"] 
    outputChatBox("index: "..i.." value x: "..x.." y: "..y.." z: "..z.." rot: "..r) 
    return x,y,z,r 
end 
  
function table.contains(t,value) 
for k,v in pairs(t) do 
if(v==value)then  
return true 
end 
end 
return false 
end 
  
addEventHandler("onResourceStart", resourceRoot, 
function () 
for i=1,3 do 
    local x,y,z,rot = getRandomizePos() 
    peds =  createPed(0,x,y,z,rot) 
     end 
end) 

Edited by Guest
Link to post
local peds = { 
    [1] = {1067.0164794922, 2035.6273193359, 10.8203125, 90},   
    [2] = {1067.0299072266, 2041.1890869141, 10.8203125, 90},   
    [3] = {-2067.029296875, 2035.0201416016, 9.8203125,90},   
    [4] = {2067.0295410156, 1032.1505126953, 10.8203125,90},   
    [5] = {1067.0295410156, 2027.8729248047, 10.8203125,90} 
} 
local createdPeds = {} 
  
addEventHandler("onResourceStart", resourceRoot, function () 
    for i=1, 3 do 
         local rnd = math.random ( #peds ) 
         while createdPeds[rnd] do 
              rnd = math.random ( #peds ) 
         end     
         createdPeds[rnd] = createPed ( 0, peds[rnd][1], peds[rnd][2], peds[rnd][3], peds[rnd][4] ) 
     end 
end) 

Link to post

i make some changes now work:

local repeteadIndex = {} 
  
local peds = { 
    [1] = {x=1067.0164794922,y=2035.6273193359, z=10.8203125, rot=90},   
    [2] = {x=1067.0299072266,y=2041.1890869141, z=10.8203125, rot=90},   
    [3] = {x=-2067.029296875,y=2035.0201416016, z=9.8203125,rot=90},   
    [4] = {x=2067.0295410156,y=1032.1505126953, z=10.8203125,rot=90},   
    [5] = {x=1067.0295410156,y=2027.8729248047, z=10.8203125,rot=90} -- the last dont have ,   
} 
  
function getRandomizePos() 
    local i 
    repeat i = math.random(#peds) 
    until(table.contains(repeteadIndex,i)==false) 
    table.insert(repeteadIndex,i) 
     
    local x,y,z,r = peds[i]["x"],peds[i]["y"],peds[i]["z"],peds[i]["rot"] 
    outputChatBox("index: "..i.." value x: "..x.." y: "..y.." z: "..z.." rot: "..r) 
    return x,y,z,r 
end 
  
function table.contains(t,value) 
for k,v in pairs(t) do 
if(v==value)then  
return true 
end 
end 
return false 
end 
  
addEventHandler("onResourceStart", resourceRoot, 
function () 
for i=1,3 do 
    local x,y,z,rot = getRandomizePos() 
    local ped =  createPed(0,x,y,z,rot) 
     end 
end) 

but Bonus code is better

Link to post

you can use my code or edit Bonus code like this:

local peds = { 
    [1] = {1067.0164794922, 2035.6273193359, 10.8203125, 90},   
    [2] = {1067.0299072266, 2041.1890869141, 10.8203125, 90},   
    [3] = {-2067.029296875, 2035.0201416016, 9.8203125,90},   
    [4] = {2067.0295410156, 1032.1505126953, 10.8203125,90},   
    [5] = {1067.0295410156, 2027.8729248047, 10.8203125,90} 
} 
local createdPeds = {} 
  
addEventHandler("onResourceStart", resourceRoot, function () 
    for i=1, 3 do 
     local x,y,z,rot = getRandomizePos() 
-- now you can use x,y,z,rot variables    
      local ped = createPed (0,x,y,z,rot) 
      --createBlip(x,y,z) 
     end 
end) 
  
function getRandomizePos() 
local rnd = math.random ( #peds ) 
  while createdPeds[rnd] do 
    rnd = math.random ( #peds ) 
  end 
    table.insert(createdPeds,rnd) 
    local x,y,z,rot = peds[rnd][1],peds[rnd][2],peds[rnd][3],peds[rnd][4] 
    return x,y,z,rot 
end 

Link to post

Still doesn't work..

Outputing two index of 5 are the same

like: 5,2,2,1,3

My new code:

local peds = {} 
local createdPeds = {} 
local createPeds = { 
    [1] = {1067.0164794922, 2035.6273193359, 10.8203125, 90},   
    [2] = {1067.0299072266, 2041.1890869141, 10.8203125, 90},   
    [3] = {-2067.029296875, 2035.0201416016, 9.8203125,90},   
    [4] = {2067.0295410156, 1032.1505126953, 10.8203125,90},   
    [5] = {1067.0295410156, 2027.8729248047, 10.8203125,90} 
} 
  
function getRandomizePos() 
local rnd = math.random ( #createPeds ) 
  while createdPeds[rnd] do 
    rnd = math.random ( #createPeds ) 
  end 
    table.insert(createdPeds,rnd) 
    local x,y,z,rot = createPeds[rnd][1],createPeds[rnd][2],createPeds[rnd][3],createPeds[rnd][4] 
    return x,y,z,rot,rnd 
end 
  
addEventHandler("onResourceStart", resourceRoot,  
function () 
    for index, v in pairs(dealerPeds) do 
        local x,y,z,rot,rnd = getRandomizePos() 
        local int = rnd 
        peds[int] = createPed(0,x,y,z)   
        if peds[int] then 
            setElementRotation(peds[int],0,0,rot) 
        end   
        createBlip(x,y,z,6,2,255,0,0,0,0,600) 
    end  
end 

Link to post

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