Task.wait() being a bit off between functions

i fully dont know how to explain it so ill just give the code here

--this function is going to be here to put the numbers on a certain gui
function timer(timee)
	task.spawn(function()
		for i = timee, 0, -1 do	
			task.wait(1)
			workspace.time.Value = i
			game.ReplicatedStorage.remoteevents.settimer:FireAllClients(i)
			--this remote event is used on clients to set the gui text to the current time on the timer
			print("second", tostring(i))
			--also here i dont know if its too important buut the thing printed in the output is the same as the timer. dunno if its needed but i thought id just put it out here
		end
	end)
end

function function1()
	print("function1 started")
	timer(20)
	spawnsomething()
	task.wait(2)
	spawnsomething()
	task.wait(2)
	spawnsomething()
	task.wait(2)
	spawnsomething()
	task.wait(14)
	--in theory those task.wait() instances should add up to 20, just like the timer, but for some reason the next functions begins before the timer ends
	function2()
end

function function2()
	print("wave 2 started")
	timer(30)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(23)
	--here, again, the task.wait()s should be added up to be 30, identically to the timer, but the functions still ends before the timer does.
end

it should be pretty clear that i want to make the task.wait()s end at the same time that the timers end to, aand they dont. please help.

also that is pretty bad code from my end but just please stick with it. i know its bad but just try to ignore it lol

I tried your code in a game and here’s what I did to fix it,

1 - On line 4, I replaced the the for i = timee, 0, -1 do to → the for i = timee, 1, -1 do

Explanation: Because of the way for i loops work, if you put "the for i = timee, 0, -1 do " it will yield 1 second longer that of your timee variable stands for.

2 - On line 5, I changed the task.wait(1)'s place to the end of the for i loop

Explanation: When the code enters the for I loop, it will yield 1 second before the actual code runs, instead what I did was to run the code then yield for 1 second.

--this function is going to be here to put the numbers on a certain gui
function timer(timee)
	task.spawn(function()
		for i = timee, 1, -1 do	
			workspace.time.Value = i
			game.ReplicatedStorage.remoteevents.settimer:FireAllClients(i)
			--this remote event is used on clients to set the gui text to the current time on the timer
			print("second", tostring(i))
			task.wait(1)
			--also here i dont know if its too important buut the thing printed in the output is the same as the timer. dunno if its needed but i thought id just put it out here
		end
	end)
end

function function1()
	print("function1 started")
	timer(20)
	spawnsomething()
	task.wait(2)
	spawnsomething()
	task.wait(2)
	spawnsomething()
	task.wait(2)
	spawnsomething()
	task.wait(14)
	--in theory those task.wait() instances should add up to 20, just like the timer, but for some reason the next functions begins before the timer ends
	function2()
end

function function2()
	print("wave 2 started")
	timer(30)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(1)
	spawnsomething()
	task.wait(23)
	--here, again, the task.wait()s should be added up to be 30, identically to the timer, but the functions still ends before the timer does.
end

thanks!! ill try using that soon

1 Like

that didnt quite work. although the timer is now almost synced with the task.waits of the other functions, its still a biit off. maybe its that task.wait delay ive heard about some time before, where the task.wait is delayed by around 29 milliseconds. not sure if thats the cause nor if thats true or not.

Try this

function timer(timee)
    task.spawn(function()
        local startTime = tick()
        while tick() - startTime <= timee do
            local remainingTime = math.ceil(timee - (tick() - startTime))
            workspace.time.Value = remainingTime
            game.ReplicatedStorage.remoteevents.settimer:FireAllClients(remainingTime)
            print("second", tostring(remainingTime))
            task.wait(1)
        end
        workspace.time.Value = 0  
    end)
end

function function1()
    print("function1 started")
    timer(20)
    spawnsomething()
    task.wait(2)
    spawnsomething()
    task.wait(2)
    spawnsomething()
    task.wait(2)
    spawnsomething()
    task.wait(14)
    function2()
end

function function2()
    print("wave 2 started")
    timer(30)
    spawnsomething()
    task.wait(1)
    spawnsomething()
    task.wait(1)
    spawnsomething()
    task.wait(1)
    spawnsomething()
    task.wait(1)
    spawnsomething()
    task.wait(1)
    spawnsomething()
    task.wait(1)
    spawnsomething()
    task.wait(23)
end

will try, thanks for the reply

oh hey, that had actually worked!! thanks a bunch

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.