Using coroutine.resume doesn't redo the function

  1. **What do you want to achieve?
    I want to make the States() function coroutine to go to coroutine.yield every time KeyPress event is fired
  2. **What is the issue?
    doing coroutine.resume doesn’t make the coroutine go to coroutine.yield
    but it just makes wait() in coroutine duplicated.
  3. **What solutions have you tried so far?
    I tried using only the function, and it didn’t redo the function everytime I called it.
    So I decided to use coroutine, and this is the code I made.

local KeyPress = script.Parent.Keys.KeyPress

local function States()
	while true do
		print("loop started")
		while true do
			for i = 1, 20 do
				print(i)
				wait(1)
			end
		end
		coroutine.yield()
	end
end

local CT = coroutine.create(States)

coroutine.resume(CT)

KeyPress.OnServerEvent:Connect(function()
	if not CT or coroutine.status(CT) == "dead" then
		CT = coroutine.create(States)
	end
	coroutine.resume(CT)
end)