Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can someone help me with wait?

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.

3 answers

We instead check constantly, but keep track of how much time has passed. Here's two ways to do it:

local start = tick();
while tick() - start > 18000 and not over do
	wait(1);
end
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 overcondition 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)

2 comments

0

Aww, you did implement my idea in an edit. By the way, pretty sure delay first argument must be a number.

0

So, lets say in my game, either a value turns true or 18000 seconds pass. Where would I put the "if game.Workspace.Value.Value==true then" or whatever

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.