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

Game can't tell the amount of players on the server?

Asked by 4 years ago

Hello! I was making a global timer that sets off other events and I didn't get very far before seeing an error that didn't pop up in the output. Off the bat I know there are probably many errors in this, but one thing that stands out is the game can't tell how many players are in the game. It just keeps doing the loop and printing (Not enough players). I tested this on a server with 3 people, but it still doesn't work.

Here's the script:

  • Server Script
  • In ServerScriptService (Workspace doesn't change anything)
--Timers
local r = 25 --Before match
local g = 420 --During Match 
local b = 600 --During Boss
local c = 15 --Waiting for players
local t 
local team = game.Teams.Fighting:GetChildren()
local teamc = #team
--Variables
local players = game.Players:GetPlayers()

--Code Start
local function init ()
    t = tick()
end

while true do 
    print("Intermission")
    wait(r)
    if #players >= 2 then
        print("Match Started")
        repeat


        local curt = tick()
        local match = t - curt
        until match > g or teamc == 1
        print("Match Ended")
    else if #players < 2 then
        print("Not Enough Players")
        end
    end
end

Thanks for looking!

2 answers

Log in to vote
1
Answered by 4 years ago

The problem is you're setting the number of players upon the server's creation and then never changing it in the loop. So when the first player joins, #players will always remain 1 forever because it isn't being updated. To fix this, simply include it in the loop. To help you out, I'm also going to take out the obsolete stuff, but feel free to add it back if desired.

--// Settings //--

local r = 25; --Before match
local g = 420; --During Match 
local b = 600; --During Boss
local c = 15; --Waiting for players

--// Main Loop //--

while true do 

    local team = game.Teams.Fighting:GetChildren();
    local teamc = #team;

    local players = game.Players:GetPlayers();

    local t = tick();

    while #players < c do
        players = game.Players:GetPlayers();

    wait();
    end

    print("Intermission");

    wait(r);

        local curt = t;
        local match = t - curt; --// I don't know what the purpose of this is. Will return either nil or 0

        until match > g or teamc == 1
             print("Match Ended")
        end

    end
end

0
Thanks! It I was getting confused on why putting Player in the loop would help, but then I relized it gets the player amount every time the loop happens and replaces the old variable. fighterkirbyzx 102 — 4y
0
It doesn't replace the old variable because `players` only exists within that scope. Once the loop repeats, it leaves that scope, removing it, and then you redefine it. hiimgoodpack 2009 — 4y
Ad
Log in to vote
1
Answered by
Hypgnosis 186
4 years ago

You are getting the amount of players only once, when the script first runs. So players will always be a table of one value - the first player who joined the server.

You need to GetPlayers() at the start of every loop:

--Timers
local r = 25 --Before match
local g = 420 --During Match 
local b = 600 --During Boss
local c = 15 --Waiting for players
local t 
local team = game.Teams.Fighting:GetChildren()
local teamc = #team


--Code Start
local function init ()
    t = tick()
end

while true do 
    print("Intermission")
    wait(r)
    local players = game.Players:GetPlayers()
    if #players >= 2 then
        print("Match Started")
        repeat


        local curt = tick()
        local match = t - curt
        until match > g or teamc == 1
        print("Match Ended")
    else if #players < 2 then
        print("Not Enough Players")
        end
    end
end
0
Yep! It works! Thank you both for the answer (I could only accept on of you) fighterkirbyzx 102 — 4y

Answer this question