A library which helps you to loop your tables in a defined time.
This library will help you to loop tables in a defined time. Let's say you want to loop a table and it must take 30 seconds to finish. Well, this library does that. For instance, it will only process tables which have numbers as index. (ipairs table)
It couldn't be simpler. Create a file on your resource's folder and paste the content of the smoothloop.lua
, located in this repository. The type can be client, server or shared. It's totally up to you. Ensure you are loading this file first!
SmoothLoop.new(table, msToFinish, callback):run()
Your table to process.
Time, in milliseconds, for the loop to finish.
Your callback function, which will give in the parameters, the value and the index, respectively.
To run the loop, you must add the run
method in the final of the call of the function. Of course, you could store it in a variable and use it later. Example:
local myStuff = SmoothLoop.new(table, msToFinish, callback)
So, in another moment, you would use:
myStuff:run()
SmoothLoop.new(getElementsByType("vehicle"), 5000, function(v, i)
if v and isElement(v) then
local r, g, b = math.random(255), math.random(255), math.random(255)
setVehicleColor(v, r, g, b)
iprint("Alterando a cor do veículo " .. getVehicleName(v))
end
end):run()
This example will change the vehicles color. The loop will take 5 seconds (5000 ms) to finish. So, doing some math, let's say we have 50 vehicles on the server. The math would be:
5000 / 50
, which results in 100
. The result will be, internally, the interval time of the setTimer
function, that is, 100ms for each vehicle.
androksi ❤️