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

How would i stop a loop if a certain value is false?

Asked by 7 years ago
Edited 7 years ago

I am trying to make this loop stop once all players in the server has a value as false. Would this work?...

for i = 120,1,-1 do
        Status.Value = i
                if game.Players:GetChildren().PlayerValues.Playing == false then
                break 
               end
        wait(1)
    end
0
I think I understand what you're trying to accomplish, and I am able to help you out. However you haven't exactly helped me out by explaining your issue and what you plan to achieve. If you could revise your question to resolve these concerns I will be able to help you out. M39a9am3R 3210 — 7y
0
You do create the value objects and set them to true at the start of the loop right? I will assume you do for the answer. M39a9am3R 3210 — 7y
0
Yeah i do User#13357 0 — 7y
0
In your main loop, you can create another loop where you loop through all the players, and if one of them is still playing, you continue the main loop, otherwise, break the loop. GoldenPhysics 474 — 7y

1 answer

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

Problem

There are a couple of problems with your script. First off, GetChildren returns a table. If you try to use .PlayerValues on a table, the script will think "oh you're looking for an object named PlayerValues right in Players". Then the script will say, "nope nothing named PlayerValues here, I have an error".


Solution

So you want to make a function that will loop through all players in Players service to determine if there is a PlayerValues value still set to true.


Breakdown

In the new script I've made below we are establishing a function that we can use to return a true or false value, if there is anyone with a value of true or not. Within the function there is a for loop that will go through all Player instances in the Players service. With that loop it will go into each individual player and check if the value is true, if so return false to the if then statement. If the loop is to finish then that means that no one has a value of true and the script will return to the if then statement with true. This will cause your code in the if then statement to execute and break your numerical for loop.


Final Script

function AllPlayersFalse() --Establishing function.
    for i,v in pairs(game.Players:GetPlayers()) do --Go through all players.
        if v.PlayerValues.Playing.Value == true then --Check if Playing value is true.
            return false --Since we're checking for falses, this has to return false as that means a person is still playing.
        end
    end
    return true --The for loop is done, no one is playing.
end


for i = 120,1,-1 do
    Status.Value = i
    if AllPlayersFalse() then --Have the function return true or false to complete the if then statement.
        break 
    end
    wait(1)
end

Hopefully this answered your question. If so, do not forget to hit the accept answer button. If you have any other questions, feel free to leave them in the comments below. If the script errors out, then please leave the error in the comments below along with the line number it is occurring on and I will see what I can do.
0
Did you consider continue statements? GoldenPhysics 474 — 7y
0
It works in studio but not in server User#13357 0 — 7y
0
What's the location of the script? What type of script is it? M39a9am3R 3210 — 7y
0
Its a regular script and its in serverscriptservice User#13357 0 — 7y
View all comments (3 more)
0
Any errors in the Developer console on online mode? Hit F9 and click on the Server Logs tab, look for any red messages coming from the script. M39a9am3R 3210 — 7y
0
Onserverinvoke errors User#13357 0 — 7y
0
Well then, you probably will need to make another question as to why OnServerInvoke is erroring for you. You would have to post the full error and show the code of which is affected. Chances are if it's working in Solo mode but not Server, then you accidentally flip flopped the places of callbacks and functions with Server scrips and Local script. M39a9am3R 3210 — 7y
Ad

Answer this question