..:D&G:.. Posted February 21, 2016 Share Posted February 21, 2016 Hello guys, is there any way to have a timer and change/load an object every 2 seconds from ipairs? Because sometimes the server freezes when there are over 100+ vehicles or peds etc... Example: for i, v in ipairs(getElementsByType("vehicle")) do setElementData(v, "data", 1) end For example, that code will send the data of all the vehicles from the server, but there are 500 vehicles (just as an example ) but the data will change for all of them at once, is there anyway I can set the data for like 1 vehicle every 2 seconds or 5 vehicles every 2 seconds? Thanks. Link to comment
Bonus Posted February 21, 2016 Share Posted February 21, 2016 Sure: local counter = 0 setTimer ( function ( ) local vehicles = getElementsByType ( "vehicle" ) if vehicles[1] then counter = #vehicles > counter and counter + 1 or 1 setElementData ( vehicles[counter], "data", 1 ) end end, 2000, 0 ) Link to comment
..:D&G:.. Posted February 21, 2016 Author Share Posted February 21, 2016 Doesn't that require a killTimer? Link to comment
Bonus Posted February 21, 2016 Share Posted February 21, 2016 local counter = 0 local vehicletable = nil addCommandHandler ( "dothatthing", function ( ) vehicletable = getElementsByType ( "vehicle" ) counter = 0 if vehicletable[1] then setTimer ( function ( ) counter = counter + 1 if isElement ( vehicletable[counter] ) then setElementData ( vehicletable[counter], "data", 1 ) end end, 2000, #vehicletable ) end 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