Need help with some math

So I have this function

local function flyOre(ore, target, duration) --Lets say duration = 3
    local pos0 = ore.Position
    local pos1 = (pos0 + target.Position) / 2 + Vector3.new(0, 10, 0)
    local pos2
    
    duration = ..idk how to calculate this

    for i = 0, 1, duration do
        pos2 = target.Handle.Position
        ore.Position = QuadraticBezier(t, pos0, pos1, pos2)
        task.wait()
    end
end

Does anyone know how to calculate the duration?
For example if I set the duration to 3 in the function, that means the for i loop should take 3 seconds to complete (this has to be achieved by changing the 3rd parameter in the for i loop)

you can change the for loop into a while loop using task.wait(0.1) until a counter is greater than duration

local start = os.time()
local duration = 3
local t = 0
while t < duration do
    task.wait(0.1)
    t += 1
end
print(os.difftime(os.time(), start)) 

output: 3

if you really need it to move every frame with no delay, place the task.wait and t += 1 in a coroutine.wrap so it runs on a different thread than the waiting

local start = os.time()
local duration = 3
local t = 0
while t < duration do
    coroutine.wrap(function()
        task.wait(0.1) -- if you do it this way you can just do task.wait(1)
        t += 1
   end)()
   -- movement code
   task.wait()
end
print(os.difftime(os.time(), start)) 
1 Like

Thanks!
I’ve tried what you suggested and because of them my github copilot generated something that works even better :open_mouth:

local start = os.clock()
local t = 0
while t < 1 do
   t = (os.clock() - start)/duration
   part.Position = QuadraticBezier(t, pos0, pos1, box.Handle.Position)
   task.wait()
end