Constantly checking

So I need help on constant checking I want if there is enough things in a table then run some code but while it is running the code I want it constantly checking if the table doesn’t have enough things and if it doesn’t then stop running the code and go back to the begging

So you could do something like this

    local tableOfElements = { }
    local numOfElementsInTable = 0

    local numberOfElementsToPerformLoop = 10

    function addElementToTable(elementToAdd)
        table.insert(tableOfElements, elementToAdd)
        numOfElementsInTable = numOfElementsInTable + 1
    end

    function removeElementFromTableByIndex(index)
        table.remove(tableOfElements, index)
        numOfElementsInTable = numOfElementsInTable - 1
    end

    function removeElementFromTableByElement(element)
        local indexOfElement = table.find(tableOfElements, element)

        if indexOfElement then
             table.remove(tableOfElements, indexOfElement)
             numOfElementsInTable = numOfElementsInTable - 1
        else
             print("Element Not Found in function - Remove Element From Table by Element")
        end
    end

    while true do
        if numOfElementsInTable >= numberOfElementsToPerformLoop then
            --Perform Code which runs when enough elements are in table
        end
    end

Hope this is what you were looking for :slight_smile:

Just a little more context. If you use the functions to add and remove elements from the table then the while loop does not need to perform its own checks since the variable storing the number of elements would be getting updated externally.

so i find this a little confusing could you exsplain

Do while loops constantly check?

also is there a way to add a else to a while statment

So while loops run until their condition is met. And you could add an else statement to the if which is inside the while loop

While     ConditionGoesHere      do

end

So if players is not enough tell status to show that
if there is enough players then start the round
but i want if it goes back to not enough players while the round is going to then go back to the beggining and tp the players to the lobby

local function RoundTimer()
	while wait() do
		if #playing < playersNeded then
			wait(playerCheck)
			status.Value = "Need more players"
		else
			while #playing >= playersNeded do
				wait(playerCheck)

So the second while loop should not need to be there since the first one will always be running

step by step it will go

while wait() do
    if #playing < playersNeded then

    else
        --This code should run at any point that the above if condition is not true. This should remove the need for the second while loop
    end
end

yes i recently added that as a check if #playing ever changes but i found that it doesnt work

what i need to do is have it at the top but i hvae the else statment that holds it back

If you think there is a better way of doing this i would be glad to hear it, i am just doing this so that if everyone else leaves the game then that 1 player left doesnt have to play by themselves

You should only need one While loop since the second while loop can only be started if the “if” condition returns false. And should it ever return true, you would want to stop the other loop anyways.

while wait() do
    if #playing < playersNeeded then
        --Always runs if there are not enough players

        --This section would be used to stop the game - E.G. return to the lobby of a game
    else
        --Always runs if there are enough players

        --This section would be used to start the game - E.g. Map Voting or something
    end
end

so the problem is that the game runs in the else and the time does to so technically the while takes like a minute to run

Yeah, so I would recommend only using this piece of code to tell the game if it should start or stop. The game should probably be handled in different functions or loops to help split the logic up a bit.

Ok thankyou, I have decided to hold off until i make the win conditions because it might make it a little easier but I will try to put them in separate functions, I tried it before I don’t remember what went wrong but I might be able to figure it out thanks anyways for you help

No problem :slightly_smiling_face:, best of luck with your project. Feel free to reply to this again if you need any further help.

When creating your game logic it might help toto write it down step by step to make sure its going to do what you think it will.

See you :wave:

ok great thanks for all the help

So quick question this is off topic, if I use a pcall and it doesn’t return anything will it error?

So pcall can be used to prevent a script from completely stopping. It basically returns a bool and an errorMessage if it did not succeed.

local success, err = pcall(function()
    print(nil + 5)  --This will make the pcall return false since you can't add 5 to nil
end)
 
if not success then
   return warn(err)
end

so basically I am trying to go through two different tables to see if one or the other the player is in that table using table.find but it will always error once because the player is in either one or the other. so basically I need it to go through both tables and whatever one it is in I need it to remove the player from the table