Kelly003 Posted August 6, 2021 Share Posted August 6, 2021 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
DiSaMe Posted August 6, 2021 Share Posted August 6, 2021 (edited) 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 August 6, 2021 by CrystalMV Link to comment
Kelly003 Posted August 6, 2021 Author Share Posted August 6, 2021 There is no error and nothing is created Link to comment
CastiaL Posted August 8, 2021 Share Posted August 8, 2021 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
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