#Need help with task.delay()

1 messages · Page 1 of 1 (latest)

coarse shale
#

So what I'm trying to achieve is, when I change the time the time for the task.delay also changes instantly like

local Stime = TimeVariable.Value

TimeVariable.Value.Changed:Connect(function()
    Stime = TimeVariable.Value
end)

task.delay(Stime,---function) -- I want this time it waits to be the new time without refiring it

I hope I explained it well

delicate sparrow
iron escarp
#

that will refire it

#

"I want this time it waits to be the new time without refiring it"

coarse shale
#

Is there any way or is it impossible

delicate sparrow
iron escarp
coarse shale
#

Like

#

When its waiting the x amount of time

#

or middle of the function

#

Like

#

I have a plant growing script

#

And when I water it I want it to grow faster

delicate sparrow
#

grow a garden clone 😮‍💨

iron escarp
#

if the function is already running

coarse shale
#

Im just remaking it to get better man

#

What should i try using then

round current
#

lol pasted the wrong msg

iron escarp
#

I dont really get what u mean

coarse shale
#

No no

#

Its the same function

#

Okay so

#

lemme write a better example

round current
#

here’s a workaround you can use with task.spawn() and a manual loop instead of task.delay():

local TimeVariable = script.TimeVariable
local finished = false

task.spawn(function()
    local start = tick()
    while not finished do
        local elapsed = tick() - start
        local waitTime = TimeVariable.Value

        if elapsed >= waitTime then
            break
        end

        task.wait(0.1) -- check every 0.1 seconds
    end

    -- Your function runs here
    print("Time's up!")
end)

-- Optional: You can set `finished = true` if you want to cancel it early
#

check if it works

iron escarp
#

if the delay has ended and the function has already ran then you cant really change it 🤷‍♂️

coarse shale
#
local Time = 5
local Interval = Time/2

Time.Changed:Connect(function()
     Interval = Time/2
end)
task.delay(Interval,function()
      task.wait(Time)
      print("Code")
 end)
#

kinda like this

iron escarp
#

btw

#

.Changed wont work on a number

#

use a intvalue or sm

#

what im saying is

#

just cancel the function

#

and start a new one

#

with less edlay

#

like ill show

coarse shale
#

Ill try

#

But the system im using is kinda like grow a garden and it grows bit by bit

#

So when I rerun the function it just kinda regrows the unfinished plant

iron escarp
#
local Time = nil -- Some kind of int value or sm idk
local Interval = Time/2
local Task

function Hehe()

end

Time:GetPropertyChangedSignal("Value"):Connect(function()
     Interval = Time/2
     if Task then task.cancel(Task) end
     Task = task.delay(Interval, Hehe)
end)
Task = task.delay(Interval, Hehe)
#

When time's value is changed it cancels the first function that was ran

#

and runs another one

#

with less delay

coarse shale
#

Wait lemme try it first

#

uhh

#

it breaks the script

iron escarp
#

how

#

i mean

#

is there a error

coarse shale
#

No

iron escarp
#

oh wait

coarse shale
#

Its just that its coded liek that

iron escarp
#

what did u set time to be

coarse shale
#

Its not ab out time

#

tree looks like this when fired twice

iron escarp
#

hmm

#

probably cuz the interval was already over so the function ran before Time was changed and it wasn't canceled and the function ran it again

#

uhhh

coarse shale
#

Its coded to take the initial part shrink it and regrow it

iron escarp
#

uhhhhhhhhhhhhhhhhhh

coarse shale
#

Thats why i didnt want to fire it twice

#

Usually looks like this

iron escarp
#
local Time = 0 --settime
local Interval = Time/2
local RunService = game:GetService("RunService")
local HeartBeatFunction = nil 

function Idk()
 if HeartBeatFunction then
  HeartBeatFunction:Disconnect()
 end 

--blahblah
end

local Task = task.delay(Interval,Idk)
HeartBeatFunction = RunService.HeartBeat:Connect(function()
 Interval = Time/2
 if Task then task.cancel(Task) end
 Task = task.delay(Interval, Idk)
end)
#

yea I have no idea

coarse shale
#

Ohhh wait

#

I have an idea

#

Thanks for all the help though

coarse shale
#

Ill just replicate the model same to same

#

And then grow it faster

iron escarp
#

ok

coarse shale
#

Thanks though

#

Omg

#

I just forgot I had the growth% value