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

Script skips line where it checks if there are a certain amoutn of players?

Asked by 3 years ago

Ok so hi. Recently i have been making a game like "Giant survival". I have a line of code in my while loopt that checks if there are a ceratin amount of players. But when i test it in studio it skips it and just continues on with the main code.

Code:

while true do
if #game.Players:GetPlayers() < 2 then
    print("Need more players")
end

-- // Continue here
wait()
end
0
Is it printing anything? also, something i would do and you should try is do Players:GetChildren() instead of Players:GetPlayers() munkuush 22 — 3y
0
I think the reason it isnt working because all youre doing is just doing a print statement, what you could do isafter it is: elseif #game.Players:GetPlayers() > 2 then [your code here] end since you dont have anything checking if its more than 2 players munkuush 22 — 3y

2 answers

Log in to vote
0
Answered by
Benbebop 1049 Moderation Voter
3 years ago
Edited 3 years ago

I think I got what you are trying to do.

Pause the game while there are not enough players in the server.

Your code is failing because of your use of if. if simply reads the value its given and runs code if its true, continues if its not. In order to pause the code until there are enough players you will need a repeating function. You can approach this many different ways.

repeat loop method

repeat
    print("Need more players")
    wait()
until #game.Players:GetPlayers() > 2

This requires a check beforehand if you don't want to have it print Need more players if there are already enough players. So we will need an if statement.

if #game.Players:GetPlayers() < 2 then
    repeat
        print("Need more players")
        wait()
    until #game.Players:GetPlayers() > 2
end

This method is starting to get pretty bulky, repeat is better utilized for "pause this code until" situations with short wait times, luckily there are other methods.

while loop method

while #game.Players:GetPlayers() < 2 do
    print("Need more players")
    wait()
end

That's it for this method. I'd recommend going with it.

There are other methods but these should be good enough to get you started. Hope this helped.

0
nooooooooooooooooooo please use PlayerAdded:Wait(), not repeat wait until ...                                       please please please imKirda 4491 — 3y
Ad
Log in to vote
0
Answered by
iuclds 720 Moderation Voter
3 years ago
local RunService = game:GetService('RunService')

while #game.Players:GetPlayers() < 2 do
    RunService.Heartbeat:Wait()
end
0
This isn't a very good way to approach this problem. An explanation would also be nice. Benbebop 1049 — 3y

Answer this question