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,
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.