Hello,
I’m wondering how I can stop task.wait() when needed in my code. I have a spawning system and it waits the respawn time each time a player dies before they respawn. The reason I need to stop it is because if the player leaves, there is no need to keep yielding the code, so I cancel the respawn.
I have tried using a repeat loop to measure the time passed to simulate a single task.wait() and made it stop if it reached it’s goal or the player left. And while that works, I’m wondering if this is the best way to do it or if there’s a better way. Here’s my code:
local function ManageRespawn(Player, Character)
local Humanoid = CharacterModule:GetHumanoid(Character)
Humanoid.Died:Connect(function()
local TimePassed = 0
local StepInterval = 0.100
repeat
TimePassed += StepInterval
task.wait(StepInterval)
until TimePassed >= Players.RespawnTime or not Player.Parent
if not Player.Parent then return end
PlayersModule:Spawn(Player)
end)
end
Any help will be appreciated!