[FOTL]Aphex Posted June 21, 2011 Share Posted June 21, 2011 Hi all, I've been trying to create a random vehicle script for use in a race map. I have tested it locally and it works OK but I've been told that it is buggy when uploaded to a server in the sense that sometimes the vehicles don't change. local CP1 = createMarker (2642.626, -1115.870, -999, "checkpoint", 6.25, 255, 200, 0, 0) local CP2 = createMarker (2642.812, -1258.972, -999, "checkpoint", 6.25, 255, 200, 0, 0) local CP3 = createMarker (2643.045, -1495.755, -999, "checkpoint", 6.25, 255, 200, 0, 0) local CP4 = createMarker (2821.937, -1657.358, -999, "checkpoint", 6.25, 255, 200, 0, 0) local CP5 = createMarker (2838.349, -1092.804, -999, "checkpoint", 6.25, 255, 200, 0, 0) local CP6 = createMarker (2886.200, -605.426, -999, "checkpoint", 6.25, 255, 200, 0, 0) local CP7 = createMarker (2740.237, -177.5, -999, "checkpoint", 9.25, 255, 200, 0, 0) local CP8 = createMarker (2767.004, 121.403, -999, "checkpoint", 14.25, 255, 200, 0, 0) local CP9 = createMarker (2687.881, 360.221, -999, "checkpoint", 24.25, 255, 200, 0, 0) local CP10 = createMarker (2195.303, 470.810, -999, "checkpoint", 14.25, 255, 200, 0, 0) local CP11 = createMarker (2170.537, 226.546, -999, "checkpoint", 14.25, 255, 200, 0, 0) local CP12 = createMarker (2240.647, -207.770, -999, "checkpoint", 14.25, 255, 200, 0, 0) local CP13 = createMarker (2343.595, -85.403, -999, "checkpoint", 6.25, 255, 200, 0, 0) local CP14 = createMarker (2343.689, 91.9994, -999, "checkpoint", 6.25, 255, 200, 0, 0) local CP15 = createMarker (2343.988, 343.850, -999, "checkpoint", 6.25, 255, 200, 0, 0) local CP16 = createMarker (2344.120, 759.821, -999, "checkpoint", 14.25, 255, 200, 0, 0) local CP17 = createMarker (2477.256, 1042.617, -999, "checkpoint", 14.25, 255, 200, 0, 0) local CPCount = 0 local carids = {429, 541, 415, 480, 562, 565, 434, 494, 502, 503, 411, 559, 561, 560, 506, 451, 558, 555, 477, 602, 496, 401, 518, 527, 589, 419, 533, 526, 474, 545, 517, 410, 600, 436, 580, 439, 549, 491, 445, 604, 507, 585, 587, 466, 492, 546, 551, 516, 467, 426, 547, 405, 409, 550, 566, 540, 421, 529, 581, 509, 481, 462, 521, 463, 510, 522, 461, 448, 468, 586, 485, 552, 431, 438, 437, 574, 420, 525, 408, 416, 433, 427, 490, 528, 407, 544, 523, 470, 598, 596, 597, 599, 432, 601, 428, 499, 609, 498, 524, 532, 578, 486, 406, 573, 455, 588, 403, 514, 423, 414, 443, 515, 531, 456, 459, 422, 482, 605, 530, 418, 572, 582, 413, 440, 543, 583, 478, 554, 536, 575, 534, 567, 535, 576, 412, 402, 542, 603, 475, 568, 424, 504, 457, 483, 508, 571, 500, 444, 556, 557, 471, 495, 539, 579, 400, 404, 489, 505, 479, 442, 458} local boatids = {472, 473, 493, 595, 484, 430, 453, 452, 446, 454} local planeids = {592, 577, 511, 548, 512, 593, 425, 520, 417, 487, 553, 488, 497, 563, 476, 447, 519, 460, 469, 513} function randomCar(player, dimension) if source == CP1 then if CPCount == 0 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 1 end end end end if source == CP2 then if CPCount == 1 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 2 end end end end if source == CP3 then if CPCount == 2 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 3 end end end end if source == CP4 then if CPCount == 3 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 4 end end end end if source == CP5 then if CPCount == 4 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 5 end end end end if source == CP6 then if CPCount == 5 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 6 end end end end if source == CP7 then if CPCount == 6 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 7 end end end end if source == CP8 then if CPCount == 7 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 8 end end end end if source == CP9 then if CPCount == 8 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, boatids[math.random(1,10)]) CPCount = 9 end end end end if source == CP10 then if CPCount == 9 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, boatids[math.random(1,10)]) CPCount = 10 end end end end if source == CP11 then if CPCount == 10 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, boatids[math.random(1,10)]) CPCount = 11 end end end end if source == CP12 then if CPCount == 11 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 12 end end end end if source == CP13 then if CPCount == 12 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 13 end end end end if source == CP14 then if CPCount == 13 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,161)]) CPCount = 14 end end end end if source == CP15 then if CPCount == 14 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, carids[math.random(1,20)]) CPCount = 15 end end end end if source == CP16 then if CPCount == 15 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, planeids[math.random(1,20)]) CPCount = 16 end end end end if source == CP17 then if CPCount == 16 then if getElementType(player) == "player" then local veh = getPedOccupiedVehicle(player) if veh then setElementModel (veh, planeids[math.random(1,20)]) CPCount = 17 end end end end end addEventHandler('onMarkerHit', getRootElement(), randomCar) As it is the markers are at the same coordinates as the race checkpoints, although I have made the markers 4 sizes bigger than the CPs so they have the same active radius (i.e. if the CP size = 2.25, then the marker size = 6.25). I've also made the markers at -999 on the z axis because setting the alpha setting to zero had no effect (it was still visible). I am a novice at lua scripting btw... am I doing anything wrong? is there any way of making the script more efficient?? Please be gentle, lol, any help will be excellent! Link to comment
SDK Posted June 21, 2011 Share Posted June 21, 2011 hai thar local carids = {429, 541, 415, 480, 562, 565, 434, 494, 502, 503, 411, 559, 561, 560, 506, 451, 558, 555, 477, 602, 496, 401, 518, 527, 589, 419, 533, 526, 474, 545, 517, 410, 600, 436, 580, 439, 549, 491, 445, 604, 507, 585, 587, 466, 492, 546, 551, 516, 467, 426, 547, 405, 409, 550, 566, 540, 421, 529, 581, 509, 481, 462, 521, 463, 510, 522, 461, 448, 468, 586, 485, 552, 431, 438, 437, 574, 420, 525, 408, 416, 433, 427, 490, 528, 407, 544, 523, 470, 598, 596, 597, 599, 432, 601, 428, 499, 609, 498, 524, 532, 578, 486, 406, 573, 455, 588, 403, 514, 423, 414, 443, 515, 531, 456, 459, 422, 482, 605, 530, 418, 572, 582, 413, 440, 543, 583, 478, 554, 536, 575, 534, 567, 535, 576, 412, 402, 542, 603, 475, 568, 424, 504, 457, 483, 508, 571, 500, 444, 556, 557, 471, 495, 539, 579, 400, 404, 489, 505, 479, 442, 458} local boatids = {472, 473, 493, 595, 484, 430, 453, 452, 446, 454} local planeids = {592, 577, 511, 548, 512, 593, 425, 520, 417, 487, 553, 488, 497, 563, 476, 447, 519, 460, 469, 513} function randomCar(checkpoint, time) local veh = getPedOccupiedVehicle(source) if 1 <= checkpoint and checkpoint <= 8 then setElementModel (veh, carids[math.random(#carids)]) elseif checkpoint <= 11 then setElementModel (veh, boatids[math.random(#boatids)]) elseif checkpoint <= 15 then setElementModel (veh, carids[math.random(#carids)]) elseif checkpoint <= 17 then setElementModel (veh, planeids[math.random(#planeids)]) end end addEvent('onPlayerReachCheckpoint') addEventHandler('onPlayerReachCheckpoint', getRootElement(), randomCar) No need for all the markers, race has a checkpoint event (and you won't need to check if the player exists and has a vehicle) No need for so many if's, you can say "between those checkpoints" Link to comment
[FOTL]Aphex Posted June 21, 2011 Author Share Posted June 21, 2011 Hey SDK... Aaaaaaaah, Thanks a lot... I've been scouring the wiki - searched for "checkpoint" and nothing came up... i was just about to try out createColCirlce instead of markers lol That certainly is a lot more efficient! Thanks again. You're a gentleman and a scholar! EDIT: Can I just ask... with the math.random(#carids) - does the #carids automatically specify the amount of cells in the array? and for: function randomCar (checkpoint, time) What does time refer to? (sounds silly I know) Do I have to specify a duration??? or is it a placeholder for something I dont need? Link to comment
SDK Posted June 21, 2011 Share Posted June 21, 2011 Aaaaaaaah, Thanks a lot... I've been scouring the wiki - searched for "checkpoint" and nothing came up... i was just about to try out createColCirlce instead of markers lol Yeah, it's hidden in the resource catalogue https://wiki.multitheftauto.com/wiki/Resource:Race EDIT: Can I just ask... with the math.random(#carids) - does the #carids automatically specify the amount of cells in the array? Yup, and math.random(1,#carids) is the same as math.random(#carids). If you give one parameter for math.random, it returns a number between 1 and the parameter. and for: function randomCar (checkpoint, time) What does time refer to? (sounds silly I know) Do I have to specify a duration??? or is it a placeholder for something I dont need? time = time since the race started, in milliseconds. And yeah, it's kinda a placeholder, but you can remove it if you want. Lua will not complain if you don't define all the variables that are passed through a function. Link to comment
[FOTL]Aphex Posted June 21, 2011 Author Share Posted June 21, 2011 Thanks for the reply... and hehe, just found that hidden Resource:Race thing by googling for onPlayerReachCheckpoint Easy when you know how, eh! Thanks again. 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