|
| 22 Jan 2017 12:29 PM |
So I have a value inside of every player in my game, My question is how to I keep track of how many of those values == true?
My take on this is:
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 |
|
|
| Report Abuse |
|
|
| |
|
|
| 22 Jan 2017 12:30 PM |
local playing = {}
for _, player in next, game:GetService("Players"):GetPlayers() do if player.Playing.Value then table.insert(playing, player) end end
|
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 12:30 PM |
oh you want to count instead of using a table just increment the integer by 1 each time
|
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 12:31 PM |
| I have absolutely no clue how to keep track of this, because players will also join/leave the game, affecting the amount of those values enabled. |
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 12:31 PM |
connect the counter increment/decrement to PlayerAdded and PlayerRemoving events
|
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 12:55 PM |
| I don't want this to keep track of the number of players, I want this to keep track of how many players have the value set to true |
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 22 Jan 2017 01:34 PM |
| Loop it and it will check the players every dam second if someone leaves it wont count them. like create local count = 0 and if player.Playing.Value == true count should be count = count + 1. ez pz |
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 01:46 PM |
| I still seem to be lost after giving it a few tries |
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 01:57 PM |
| Hmm.Try making a global Value. And if player.Playing.Changed:connect and if its true Value = Value + 1 . if false Value = Value - 1 . if PlayerLeft Check all other players values. |
|
|
| Report Abuse |
|
|
Nartronic
|
  |
| Joined: 28 Aug 2014 |
| Total Posts: 916 |
|
|
| 22 Jan 2017 02:27 PM |
function GetPlayerCount() local Count = 0 local Players = game.Players:GetChildren() for _, Player in ipairs(Players) do if player.Playing == true then Count = Count + 1 end end return Count end
local PlayersReady = GetPlayerCount()
|
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 02:49 PM |
That wouldn't work as I explained.
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. |
|
|
| Report Abuse |
|
|
Wowgnomes
|
  |
| Joined: 27 Sep 2009 |
| Total Posts: 26255 |
|
|
| 22 Jan 2017 03:07 PM |
When a value changes to true, have it change the count of a numb val in server storage, if you don't want to cycle thru all the players.
|
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 05:45 PM |
@Wowgnomes, But, what about when a player leaves the game? There won't be a change in value, but a change in Player count - which will still affect the value. |
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 05:55 PM |
um?
function CountPlayersWithBoolean() local bools = 0
for i, v in pairs(game.Players:GetChildren()) do if v:findFirstChild("YourVariable") then if v.YourVariable.Value == true then bools = bools + 1 end end
return bools
end
|
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 06:01 PM |
| Again, that script will not check if players have left the game, and will not have an effect on the counter. And running that in a loop would constantly be adding 1 to my counter. |
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 06:08 PM |
local number = 0 for i, person in ipairs(game.Players:GetPlayers()) do if person.Playing then number = number + 1 else number = number - 1 end game.Players.ChildRemoved:connect(function() if person.Playing == false then number = number else number = number - 1 end end) end |
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 06:14 PM |
Well this only changes the number if the Player has the Value enabled whilst joining the game. I want the counter to be real-time, checked every second or so,
local number = game.ServerScriptService.InGame.Value
for i, person in ipairs(game.Players:GetPlayers()) do if person.Playing then number = number + 1 else number = number - 1 end game.Players.ChildRemoved:connect(function() if person.Playing == false then number = number else number = number - 1 end end) end
|
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 06:32 PM |
Simple solution:
while true do game.ServerScriptService.InGame.Value = 0 for _, v in pairs(game.Players:GetPlayers()) do if v:FindFirstChild("Playing") and v:FindFirstChild("Playing").Value == true then game.ServerScriptService.InGame.Value = game.ServerScriptService.InGame.Value + 1 end end wait() end |
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 06:45 PM |
that's such a terrible way to do it why didn't you incorporate the events like I explained to you?
|
|
|
| Report Abuse |
|
|
Wowgnomes
|
  |
| Joined: 27 Sep 2009 |
| Total Posts: 26255 |
|
|
| 22 Jan 2017 07:25 PM |
1. do the method i said, put a stringvalue of player's name as child of intvalue total 2. on player leaving, check if their name is in intvalue, if it is, remove it and subtract 1 from intvalue
|
|
|
| Report Abuse |
|
|
|
| 22 Jan 2017 07:31 PM |
using objects for this really isn't necessary
|
|
|
| Report Abuse |
|
|
Nartronic
|
  |
| Joined: 28 Aug 2014 |
| Total Posts: 916 |
|
| |
|