Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone help me with wait?

Asked by 10 years ago

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

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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 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)

0
Aww, you did implement my idea in an edit. By the way, pretty sure delay first argument must be a number. Destrings 406 — 10y
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 Tempestatem 884 — 10y
Ad
Log in to vote
0
Answered by
Ekkoh 635 Moderation Voter
10 years ago

What is that 'something'? Depending on what it is, you'll have to write your script differently.

Log in to vote
0
Answered by
Destrings 406 Moderation Voter
10 years ago

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.

Answer this question