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

How to check how many Values are equal to true within all Players?

Asked by 7 years ago
Edited 7 years ago

I am trying to keep the track of a value within all players, but only keep track of how many of those Values == true. How would I do this? My idea was the below, but I am pretty sure I am doing this wrong, and I am unsure where to take it from there.

So: I want this to keep track of how many players have the value set to true, and this might change due to players joining/leaving the game or even by dieing. So I would like the counting to be done in real-time, and constantly updated.

while true do wait(1)
    local players = game.Players:GetChildren()
    for i, player in ipairs(game.Players:GetChildren()) do
        if player.Playing == true then
            -- keep track of how many of players have the value set to true?
        end
    end
end

Thanks,

1 answer

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

Data structures

A table is defined as a data structure for a reason - to structure your data! How you organize and store information has a big impact on efficiency, and readability. To spare you a lecture, you should use a table to store the information you're trying to access. Specifically, a dictionary. In short, a dictionary allows you to store information with variables, inside a table. This of course would be opposed to storing information inside a table, with no variables, like this: {"string", true, 123} whereas a dictionary would look something like this: {a = "string", b = true, c = 123}.

Organize

A great way to keep track of your player's data, is by using the player itself as a key in your dictionary table. For example player_data[player] = value, where player_data is your dictionary and player is the key associated with value in that dictionary. A good time to create such data, would be when the player joins and when the player leaves. This is what can also be referred to as session data, since it's temporary, and will disappear when the session (your game instance) ends. Here's an example:

-- Game services
local Players = game:GetService("Players")

-- Session data
local SessionData = {}

local function PlayerJoined(player)
    -- When player joins, use them as a new key in the table.
    -- Their key will be associated with a new table, holding their information.
    SessionData[player] = {}
end

local function PlayerLeft(player)
    -- When the player leaves, remove them and their data from the table.
    SessionData[player] = nil
end

Players.PlayerAdded:Connect(PlayerJoined)
Players.PlayerLeft:Connect(PlayerLeft)

This is a great start. Now you can look for an individual player's data, or all player data with ease! Let's try using this example in your scenario:

-- Game services
local Players = game:GetService("Players")

-- Session data
local SessionData = {}

-- Function to get a list of all players who have Playing set to true
local function GetPlayersPlaying()
    local playing = {} -- list of playing players

    -- Search through session data for players 
    for player, playerData in next, SessionData do
        if playerData.Playing then -- If Playing exists, or is true, then...
            playing[#playing + 1] = player -- Insert player in list
            print(player.Name.." is playing!")
        else
            print(player.Name.."is not playing")
        end
    end
    return playing -- return list
end

local function PlayerJoined(player)
    -- When player joins, use them as a new key in the table.
    -- Their key will be associated with a new table, holding their information.
    SessionData[player] = {
        Playing = true; -- Hold a boolean that represents 'Playing'
    }
end

local function PlayerLeft(player)
    -- When the player leaves, remove them and their data from the table.
    SessionData[player] = nil
end

Players.PlayerAdded:Connect(PlayerJoined)
Players.PlayerLeft:Connect(PlayerLeft)

Now you have a nice, organized way to get player information within a session. You can just call GetPlayersPlaying and it will return a list of players who have Playing set to true in their session data. Hope this helped, if you have any questions, let me know.

Edit

A less-documented, blatant representation of everything optimized to handle what you're asking about, would look something like this:

local Players = game:GetService("Players")
local PlayersPlaying = {}

local function GetPlayersPlaying()
    local playing = {}
    for player, isPlaying in next, SessionData do
        if isPlaying then
            playing[#playing + 1] = player
        end
    end
    return playing
end

-- Function that can set a player's playing state
local function SetPlaying(player, playing)
    PlayersPlaying[player] = playing
end

local function PlayerJoined(player)
    SetPlaying(player, true) -- doesn't have to be true right away
end

local function PlayerLeft(player)
    PlayersPlaying[player] = nil
end

Players.PlayerAdded:Connect(PlayerJoined)
Players.PlayerLeft:Connect(PlayerLeft)

Same concept, you can use GetPlayersPlaying to return a list of the players who are indeed playing, and SetPlaying to set the playing state (true or false) of a player.

1
Does it have to be such a long script to simply get that counter? Or can this be achieved through a simple script as I have written? There is no need for that, as I will not be checking for anything other than just that one value within all players. excellentAnarchy 50 — 7y
0
You shouldn't be paying attention to the length of code, if it's properly organized and optimized to handle the task you're trying to achieve. You could shorten this a bit, but it'd be the same concept and it wouldn't matter. ScriptGuider 5640 — 7y
0
If you're only looking for one value, you could also change SessionData[player] = {Playing = true} to just SessionData[player] = true, and maybe change the name of SessionData to something like PlayersPlaying ScriptGuider 5640 — 7y
0
I edited the answer at the end to shorten things a bit. ScriptGuider 5640 — 7y
View all comments (7 more)
0
With that, how do I find out how many Players in the game actually have the value set to true? How do I even print it? excellentAnarchy 50 — 7y
0
I asked for a simple code because that way I would be able to understand it easily with the knowledge I have, the codes you have provided do not tell me much, because I do not have the required knowledge to edit it. excellentAnarchy 50 — 7y
0
Then you should ask a question about what confuses you. This isn't a request site, you shouldn't be frustrated because somebody is trying to teach you something that will help you become a better programmer, and logical thinker. I've already done more than enough to get you started, now it's your turn to do something. ScriptGuider 5640 — 7y
0
I'm not frustrated. Thank you for the explanation on how to organize data. I do understand this is not a request site, and that is the reason I have provided my example code, of how I would go around finding the value I was looking for, and the amount of knowledge I have so far. My question is, how do I print the amount of players with the value set to true, within the code you have provided? excellentAnarchy 50 — 7y
0
That's all in GetPlayersPlaying. The last example before the edit demonstrated that, where you see "player.Name..' is playing'" after being followed by an if statement to check if the player is indeed playing. That statement also exists in the last example. ScriptGuider 5640 — 7y
0
I am unable to get the total of people with Playing == true excellentAnarchy 50 — 7y
0
It's literally just #GetPlayersPlaying() ScriptGuider 5640 — 7y
Ad

Answer this question