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:
local Players = game:GetService("Players")
local SessionData = {}
local function PlayerJoined(player)
SessionData[player] = {}
end
local function PlayerLeft(player)
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:
local Players = game:GetService("Players")
local SessionData = {}
local function GetPlayersPlaying()
local playing = {}
for player, playerData in next, SessionData do
if playerData.Playing then
playing[#playing + 1] = player
print(player.Name.." is playing!")
else
print(player.Name.."is not playing")
end
end
return playing
end
local function PlayerJoined(player)
SessionData[player] = {
Playing = true;
}
end
local function PlayerLeft(player)
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
local function SetPlaying(player, playing)
PlayersPlaying[player] = playing
end
local function PlayerJoined(player)
SetPlaying(player, true)
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.
11 comments
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.
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.
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
I edited the answer at the end to shorten things a bit.
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?
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.
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.
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?
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.
I am unable to get the total of people with Playing == true
It's literally just #GetPlayersPlaying()