Corountine.wrap() not working

Hey Developers,
So I wrapped a while true do loop in a corountine so I could add scripts after it and to improve speed, I’m doing this, however it’s not working. My Hi isn’t printing nor is the timer. I get no errors, here is my script:

coroutine.wrap(function()
	while true do
		print('hi')
		for i = StartTime, 1, -1 do
			print(i)
			wait(1)
		end
		wait()
	end
end)

Thanks for any help :smile:

Well, there’s nothing wrong with the code. It’s just that you’re not running the function. You have to first create the function stored inside of a variable and then call that variable. It’s like creating a regular function but not running it.

I would take a look at the coroutine.wrap section here to learn more.

local func = coroutine.wrap(function()
	while true do
		print('hi')
		for i = 10, 1, -1 do
			print(i)
			wait(1)
		end
		wait()
	end
end)

func() -- run the function
8 Likes

The closing line for a coroutine is end)(), not end)

As mentioned, call the function.

wait() is known to cause problems, use
RunService.Heartbeat:Wait() or RunService.Stepped:Wait() instead.