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

My function is supposed to check for 2 players, but when i have two players it doesnt work?

Asked by 3 years ago
local SpecTeam = game.Teams.Spectating:GetPlayers()
function CheckForPlayers()
        print "CheckForPlayers"
        while true do
            if #SpecTeam >= 2
                then InRound.Value = true
            end
        wait(0.5)
    end
end

I think the problem lies in checking the number of players on the team.

0
Are you getting an error from this? DaysWeeksAndMonths 90 — 3y
0
no PNKFALCON1 27 — 3y

4 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

The loop is unnecessary for a function that only checks if two players are in the team once. Remove the while true do and the end associated to it for your solution.

It is CheckForPlayers not WaitForPlayers. If you wanted to wait then you’d use a repeat until approach but do not yield with wait, use an event-based approach with PlayerAdded on the team to know when a player is added. It is much more efficient then using wait for yielding.

repeat
    game.Teams.Spectating.PlayerAdded:Wait()
until #plrs >= 2
0
Very nice answer, I assumed the question differently. Roger111 347 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The only answer I can think of is that you didn't actually call your function.

after the last end of that function write

CheckForPlayers()
0
the print is printing, so the function is firing. PNKFALCON1 27 — 3y
0
Ah ok, are there any errors though? DaysWeeksAndMonths 90 — 3y
0
Ah ok, are there any errors though? DaysWeeksAndMonths 90 — 3y
0
Oops, sorry for double comment DaysWeeksAndMonths 90 — 3y
0
nope PNKFALCON1 27 — 3y
Log in to vote
0
Answered by
Roger111 347 Moderation Voter
3 years ago
Edited 3 years ago

Answer: You never updated the number of players during the loop / You did not give your loop an exit when the desired condition was met / You did not call the function to start it

Fix:

local SpecTeam = game.Teams.Spectating:GetPlayers()
function CheckForPlayers()
    SpecTeam = game.Teams.Spectating:GetPlayers() -- Updates player list
    while wait(0.5) do
        if #SpecTeam >= 2 then
            InRound.Value = true
            break -- This is used to exit the while loop
        end
    end
end
CheckForPlayers() -- Start the function

More explanation: When you initialized the variable SpecTeam you set it to the value of the current players in the session (0 players, assuming the script is loaded immediately when the game starts). And you did not update the value in your loop at all. Therefore SpecTeam will remain as an empty table indefinitely. In addition to this problem you didn't provide a way for the loop to exit when your desired outcome has been reached.

Note: If you're going to copy my code, please hit "View Source" to copy it correctly.

Optimization: (A better way to do what you're doing)

function CheckForPlayers()
    repeat wait(0.5)
    until #game.Teams.Spectating:GetPlayers() >= 2
    InRound.Value = true
    return
end
CheckForPlayers()
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Try this :

local SpecTeam = 0
for i, plrs in pairs(game.Players:GetPlayers()) do
    if plrs then
        if plrs.Team == game:GetService("Teams").Spectators then
            SpecTeam = SpecTeam + 1
        end
    end
end
function CheckForPlayers()
        print "CheckForPlayers"
    if SpecTeam >= 2 then 
        InRound.Value = true
    end
        wait(0.5)
    end
end

CheckForPlayers()

Answer this question