-- Compiled with roblox-ts v1.2.7 local TS = _G[script] local RunService = game:GetService("RunService") local function fspawn(fn) return coroutine.wrap(fn)() end --[[ * * Delays execution for a set amount of time * * ```ts * // Example usage: * delayAsync(10).then(() => { * print("Waited 10 seconds to execute this!"); * }); * ``` * * You can also do * * ```ts * await delayAsync(10); * print("Waited 10 seconds!"); * ``` * * ---- * This can also be cancelled. * * @param timeout The timeout * @param useRenderStepped To use renderStepped - for plugins iirc? ]] local function delayAsync(timeout, useRenderStepped) return TS.Promise.new(function(resolve, _, onCancel) fspawn(function() local endTime = tick() + (timeout ~= nil and timeout or 1 / 60) local ticking = true onCancel(function() ticking = false end) while tick() < endTime and ticking do if useRenderStepped then RunService.RenderStepped:Wait() else RunService.Stepped:Wait() end end if tick() >= endTime then resolve({ tick(), time() }) end end) end) end local function waitUntilDescendantOfGame(instance) return TS.Promise.defer(function(resolve) while not instance:IsDescendantOf(game) do RunService.Stepped:Wait() end resolve(instance.Parent) end) end --[[ * * Replacement for `game.Debris:AddItem(x)`. * @param instance The instance to destroy * @param seconds The time to destroy it in ]] local destroyAsync = TS.async(function(instance, seconds) if seconds == nil then seconds = 0 end if seconds > 0 then TS.await(delayAsync(seconds)) end local _ = instance:IsDescendantOf(game) and instance:Destroy() end) return { fspawn = fspawn, default = delayAsync, waitUntilDescendantOfGame = waitUntilDescendantOfGame, destroyAsync = destroyAsync, }