Hi, I couldn't find a way to how to wait in script. I saw the "Task.Wait()" function but this is not suiting to my use case. Waiting for your helps. Thanks...
Can you expand on your use case a bit? Task.Wait() sounds like it should do what you need but we might need more info to go off here.
Is there an another way? Like "wait()" in Roblox.
Could you explain why Task.Wait() doesn't suit your needs? It does pretty much the same exact thing as Roblox's wait() function.
If you want to wait without blocking any other Tasks, then yes you'll have to call Task.Wait() or Task.Wait(seconds) also using Task.Spawn() - Reference
Alternatively (but this is kind of overkill in my opinion), you could create your own Timer script which runs off Events instead. Here's a simple one I wrote:
--[[
Event-based Timer.
--]]
-- Properties
local DURATION = script:GetCustomProperty("Duration")
local TIMER_ID = script:GetCustomProperty("TimerId")
local START_TIMER_EVENT_NAME = script:GetCustomProperty("StartTimerEventName")
local STOP_TIMER_EVENT_NAME = script:GetCustomProperty("StopTimerEventName")
local PAUSE_TIMER_EVENT_NAME = script:GetCustomProperty("PauseTimerEventName")
local ON_TIMER_FINISHED_EVENT_NAME = script:GetCustomProperty("OnTimerFinishedEventName")
-- Validation
if TIMER_ID == "" then
error("TimerId property cannot be an empty string.")
end
-- Variables
local currentTime = 0
local isTimerActive = false
function Tick(deltaTime)
if isTimerActive == false then return end
currentTime = currentTime + deltaTime
if currentTime >= DURATION then
isTimerActive = false
currentTime = 0
if ON_TIMER_FINISHED_EVENT_NAME ~= "" then
Events.Broadcast(ON_TIMER_FINISHED_EVENT_NAME, TIMER_ID)
end
end
end
function StartTimer(timerId)
if timerId ~= TIMER_ID then return end
isTimerActive = true
end
function StopTimer(timerId)
if timerId ~= TIMER_ID then return end
isTimerActive = false
currentTime = 0
end
function PauseTimer(timerId)
if timerId ~= TIMER_ID then return end
isTimerActive = false
end
-- Events
if START_TIMER_EVENT_NAME and START_TIMER_EVENT_NAME ~= "" then
Events.Connect(START_TIMER_EVENT_NAME, StartTimer);
end
if STOP_TIMER_EVENT_NAME and STOP_TIMER_EVENT_NAME ~= "" then
Events.Connect(STOP_TIMER_EVENT_NAME, StopTimer);
end
if PAUSE_TIMER_EVENT_NAME and PAUSE_TIMER_EVENT_NAME ~= "" then
Events.Connect(PAUSE_TIMER_EVENT_NAME, PauseTimer);
end
You can then define the settings of each individual timer in the properties window:
To start the above timer, I would call
Events.Broadcast("StartTimer", "MyCustomTimer");
To listen when the timer finishes, I would do:
function OnTimerFinished(timerId)
if timerId == "MyCustomTimer" then
-- do stuff
end
end
Events.Connect("OnTimerFinished", OnTimerFinished);
Yep second one can be a solution but core games should add a wait function as soon as possible. Also thank you for your answers.