Running loop and code at same time

count = 0

while match == true do wait(1)
count = count+1
print(count)
end

counter()

wait(30)
if match == true then
    send:FireServer(("Match over"),"All")
    for k in pairs (JoinedList) do -- Empty Table
        JoinedList [k] = nil
    end
match = false
joined = 0
end

Whenever I run this, the second half can’t run while the loop (timer) is running. Is there any way to have the second half run once (after 30 seconds) while the loop runs constantly.

1 Like

You’d need to use either coroutines, or spawn. This allows you to create a separate thread for your loop.

Edit: I think @EmeraldSlash’s solution solves your problem better, as it combines everything into 1, effecient loop.

It looks like you’d be better off putting all of that into a single loop.

local count = 0
while match do
   wait(1)
   count = count + 1
   if count == 30 then
      -- end code
      match = false
   end
end

When checking conditions, such as value == true, you can omit the == true part as the value itself will be true in that case so the condition will use that.

Also note that using local variable = instead of just variable = is more efficient as global variables (defined without local) are slow.

1 Like

local count = 0

coroutine.wrap(function()
while match do  -- don't need to see whether it == true by explicitly comparing
      count = count+1
      print(count)
    wait(1)
end
end)() -- execute

-- counter()
print("will yield ~30 seconds now")
wait(30)
if match then
    send:FireServer("Match over", "All") -- brackets not required within arguments
    for k in pairs (JoinedList) do -- Empty Table
        JoinedList [k] = nil
    end
match = false
joined = 0
end

This isn’t necessary,
you could just do:

  JoinedList = {} -- to basically erase it's contents
1 Like