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 5 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)
01--Timers
02local r = 25 --Before match
03local g = 420 --During Match
04local b = 600 --During Boss
05local c = 15 --Waiting for players
06local t
07local team = game.Teams.Fighting:GetChildren()
08local teamc = #team
09--Variables
10local players = game.Players:GetPlayers()
11 
12--Code Start
13local function init ()
14    t = tick()
15end
View all 33 lines...

Thanks for looking!

2 answers

Log in to vote
1
Answered by 5 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.

01--// Settings //--
02 
03local r = 25; --Before match
04local g = 420; --During Match
05local b = 600; --During Boss
06local c = 15; --Waiting for players
07 
08--// Main Loop //--
09 
10while true do
11 
12    local team = game.Teams.Fighting:GetChildren();
13    local teamc = #team;
14 
15    local players = game.Players:GetPlayers();
View all 37 lines...
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 — 5y
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 — 5y
Ad
Log in to vote
1
Answered by
Hypgnosis 186
5 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:

01--Timers
02local r = 25 --Before match
03local g = 420 --During Match
04local b = 600 --During Boss
05local c = 15 --Waiting for players
06local t
07local team = game.Teams.Fighting:GetChildren()
08local teamc = #team
09 
10 
11--Code Start
12local function init ()
13    t = tick()
14end
15 
View all 33 lines...
0
Yep! It works! Thank you both for the answer (I could only accept on of you) fighterkirbyzx 102 — 5y

Answer this question