Jump to content

create random vehicle


Kelly003

Recommended Posts

how to do random vehicle creation in loop? i tried like this and it doesn't work:

server side

local id = {
    {481}, 
    {462},
    {509},
}
 
local points = {
    {-1980.59,306.16,35.17}, 
    {-1981.31,299.08,35.17},
}
 
for _,v in pairs(points) do
    createVehicle(math.random(2, #id), v[1], v[2], v[3])
end
Link to comment

First of all, your id table is needlessly nested. {481}, {462} and {509} are tables on their own, so unless you're going to add some other data in addition to IDs, it's better to replace them with plain numbers:

local id = {
    481,
    462,
    509,
} 

Now, to get to the point, the table id has 3 fields, which makes #id equal to 3, therefore your math.random call generates a number 2 or 3, and uses it as vehicle model ID.

What you need to do is to generate a random number to choose the table index and then retrieve the value under that index from the table:

createVehicle(id[math.random(1, #id)], v[1], v[2], v[3])

Same but more verbose:

local idCount = #id
local index = math.random(1, idCount)
local modelId = id[index]
createVehicle(modelId, v[1], v[2], v[3])

 

Edited by CrystalMV
Link to comment

I haven't had a chance to try it but it will most likely work.

local id = {
 481,
 462,
 509,
 }

 local points = {
 {-1980.59,306.16,35.17},
 {-1981.31,299.08,35.17},
 }

 for _,v in pairs(points) do
 createVehicle(id[math.random(1, #id)], v[1], v[2], v[3])
 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...