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

player counter does not return the correct amount of player in game?

Asked by 5 years ago

I've encounter a problem where this simple player counter prints 0 instead of 1 when there is one person that is currently in the game.

local players = game.Players
local playercount = #(players:GetPlayers())




print (playercount)

Am I doing something wrong in this script?

Any help is greatly appreciated

0
did u test it in Play Solo? maybe the script ran before the player was actually added GoldAngelInDisguise 297 — 5y
0
Use PlayerAdded and a for loop to continuously run through players then “print” or whatever function you want, the value of the return ABK2017 406 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You should make a function for this so it's much clean if you know what I mean.

function get_players()
    local count = 0

    for _, a in pairs(game:service'Players':players()) do --// gets all the players
        count = count + 1 --// adds 1 to the count if a player was found
    end

    return count --// returns the number of players
end

You can then do

local amount_of_players = get_players() --// now amount_of_players will be the amount of players in your server

print(amount_of_players)
0
Accept if this helped, thanks. Cronizete 11 — 5y
0
you forgot parentheses for get_players and i think you forgot the 'Get's before service and players also capitalization. you dont need a for loop #game.Players:GetPlayers() will do in just 1 line. im not trying to pick on you just helping OP correct some mistakes in there GoldAngelInDisguise 297 — 5y
0
:players() works too? Cronizete 11 — 5y
Ad

Answer this question