So I was wondering how I would go about doing something like this:
I want to implement rounds in my game. I want it to either go for 18000 seconds, or until something happens(if a value is true or something). How would I do this?
I thought it had something to do with
while wait(18000) do
But then I realised that would loop it every 18,000 seconds.
We instead check constantly, but keep track of how much time has passed. Here's two ways to do it:
1.
local start = tick(); while tick() - start > 18000 and not over do wait(1); end
2.
local time = 0; while time < 18000 and not over do time = time + wait(1); end
(The wait time is of course changeable; just however quickly you want / need it to respond to the changing of whatever over
condition you have)
EDIT:
OR, we can intentionally have two threads race to do something first. I don't think this is a very good idea for several reasons, but it will work, most of the time, I think. It would require a little different set up, but may or may not be more elegant if a specific (ROBLOX instance) event triggers the end of the match:
local matchIdentifier = {}; function StopMatch(cause) end function endMatchIn(time,matchid) delay(function() if matchIdentifier == matchid then StopMatch("time"); end end, time); end function StartMatch() matchIdentifier = {}; endMatchIn(18000,matchIdentifier); end -- Example of ending; end whenever a new player joins game.Players.PlayerAdded:connect(function() StopMatch("join"); end);
(I do not think this is a good approach at all, but it will work)
What is that 'something'? Depending on what it is, you'll have to write your script differently.
While BlueTaslem answer is perfectly fine, we can avoid a loop doing
local executed = false function main() if not executed then print("Stuff here") executed = true --To keep track end end delay(5, main)
And if you want to execute the code before the time you just call main.