Help me this is my first time messing with coroutine functions and i need help
i wanted to run to loops at once one for the timer and another to spin a part but when the time ends
the game end function never gets called and the coroutine keeps playing
function MiniGame.RunGame()
local newGame = gameModel:Clone()
newGame.Parent = workspace
_G.TeleportPlayers(newGame.SpawnPoint.CFrame, true)
Skyadd(true)
for countDown = 5,0,-1 do
_G.gameStatus.Value = "The Cube will start to revolve in " .. countDown.." seconds Surive!!"
task.wait(1)
end
local Spin = coroutine.create(function()
local part = newGame.Moon
while task.wait() do
part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(0.002,0.002,0.002)
end
end)
coroutine.resume(Spin)
for countDown = SurvivalTime ,0,-1 do
local playersInGame = workspace.InGame:GetChildren()
if #playersInGame == 0 then
print("No players in the game. Ending mini-game.")
EndGame()
return
end
_G.gameStatus.Value = "Survive!! ("..countDown..")"
task.wait(1)
end
coroutine.yield(Spin)
Skyadd(false)
EndGame()
end
Your coroutine is not ending because coroutine.yield is not the correct function to stop it. You should modify your logic to allow the coroutine to naturally finish or use a different approach to stop it.
You need to:
Add a condition to the spinning loop to check if the game is over.
Use a flag to indicate when the game should end.
Use coroutine.close to stop the coroutine if needed.
function MiniGame.RunGame()
local newGame = gameModel:Clone()
newGame.Parent = workspace
_G.TeleportPlayers(newGame.SpawnPoint.CFrame, true)
Skyadd(true)
for countDown = 5, 0, -1 do
_G.gameStatus.Value = "The Cube will start to revolve in " .. countDown .. " seconds. Survive!!"
task.wait(1)
end
local gameActive = true
local Spin = coroutine.create(function()
local part = newGame.Moon
while gameActive do
part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(0.002, 0.002, 0.002)
task.wait()
end
end)
coroutine.resume(Spin)
for countDown = SurvivalTime, 0, -1 do
local playersInGame = workspace.InGame:GetChildren()
if #playersInGame == 0 then
print("No players in the game. Ending mini-game.")
gameActive = false
coroutine.close(Spin) -- Stop the coroutine
EndGame()
return
end
_G.gameStatus.Value = "Survive!! (" .. countDown .. ")"
task.wait(1)
end
gameActive = false
coroutine.close(Spin) -- Stop the coroutine
Skyadd(false)
EndGame()
end
In this version:
A gameActive flag is added to control the loop inside the coroutine.
The coroutine checks the gameActive flag in each iteration of the loop.
The flag is set to false when the game is over.
coroutine.close is used to stop the coroutine explicitly.