You could have a BoolValue represent the state of the event.
local Bool = script.BoolValue
local function functionNameHere()
Bool.Value = true
--Round code here.
Bool.Value = false
end
Bool.Changed:Connect(function(Value)
if Value then --In progress.
else --Not in progress.
functionNameHere()
end
end)
Bool.Value = true --Toggle value once to start the chain.
Bool.Value = false
local events = {"Test1","Test2"}
function startEvent(Name)
if Name == "Test1" then
--Event code here
--Example : print('Hello world')
elseif Name == "Test2" then
--Event code here
--Example : print('Hello world')
end
end
while true do
startEvent(events[math.random(1,#events)])
task.wait(300)
end
Itâs fine to use it like this to do something every five minutes. But as pointed out itâs better to use indentation and task.wait()
However do keep in mind that any code after this will not execute, unless you put the while loop in a task.spawn.
Similarly, if your function is yielding (that means waiting or other functions that take time), it wonât execute every 5 minutes unless the function call is wrapped in a task.spawn()
The other problem is that now there is no way to stop the loop. If needed you could put it in a function, and add a flag to help it stop. Hereâs an untested example I quickly came up with, but in general I personally like this hacky tag pattern for âpracticalâ code.
local waitTime = 300
local tag
function doLoop()
local myTag = {}
tag = myTag
while tag == myTag do
task.spawn(myFunction)
task.wait(waitTime)
end
end
task.spawn(doLoop)
task.wait(999)
task.spawn(doLoop)
task.wait(999)
tag = nil
In any case, do keep in mind that a while-loop is fine to do something simple at a regular interval, but normally most code is part of a larger whole - it can often be more elegantly made part of the âflowâ of your game than as a simple static top-level loop. So using this pattern too much (and too high-level) may make it hard to alter that flow (such as stopping the loop). Personally I usually use while(true) without breaks at most once per place, for the core game loop. Because it almost never happens that I want something to go on uncontrollably forever. (and if it does, as @Forummer points out, the implementation generally ends up as a sequence of events triggering each other instead).
I disagree with this approach. Just because it uses events doesnât mean itâs event based programming. The code you have above is basically just sequential programming that fires an event to run a function, instead of just running the function in the first place.
If something is sequential, like waiting 300 seconds then running code, you should just wait 300 seconds then run code.
If youâre really looking for the closing thing to an event based version of this code, a Heartbeat connection might be your best bet. I would just use task.wait(n) though.
Apologies for the potential pedantia but events donât exist in vanilla Lua, you need to create a handler for that (which Roblox abstracts for us with RBXScriptSignal). Lua itself is multi-paradigm and you can adopt an event-driven paradigm in your coding events but arenât native to Lua so youâre not entirely right there.
Lua isnât just about, or even at all, about events. Yielding patterns are just as fine as event patterns. In a serious project you ideally do want to use events as much as possible but there are some cases where non-terminating loops are what you need. Experiences that have continuous round timers as a core gameplay loop are often going to fit this bill.
CMIIW. Donât want to offset the threadâs focus too much for this though.
Ultimately for OPâs case itâs completely fine to use while loops since itâs for a round-based system but they may want to consider incorporating events into it so that the round can progress as in when certain things are triggered and not a static amount of time. For example, they can still have a while loop, but an event prevents the next iteration from occurring until its fired.
They allow for you to build reactionary code (event-based).
Iâve removed the âtask.wait(300)â line from my previous snippet as it doesnât fit the example, ideally any code would be executed where that line occupied (of an arbitrary length of time), with the BoolValue acting as a state indicator.
I would be sufficiently worried if you were trying to implement an event-driven paradigm in any Lua-supported language by misusing a debugging function. Hooks being reactionary to VM instructions doesnât mean itâs opening the door for event-driven programming. Youâre welcome to give an example if you think so though.