Anyone know how I'd go about making a script detecting how many players are in server, then firing once all are doing something?

Hey, I was hoping someone on here might have an answer to what I am looking for. I am wondering if anyone knows how I could make a script that sees how many people are in a server, (under 5) and once all are say, “sleeping” in a bed, a remoteevent fires.

This isn’t in too much relation to the script, but the game is about players experiencing new things each day, and once they sleep, a new day begins. (old scripts stop executing, new scripts begin to execute)

2 Likes

You can use a remote event, let’s call it Sleep for example. In the handler, you can compare the number of how many people are on the server, and how many people are currently sleeping. It might look something like this

local SleepingPlayers = {}

game.ReplicatedStorage.Sleep.OnServerEvent:Connect(function(Player, IsSleep)
   -- // We check whether the player is going to fall asleep or wake up. If false, the player wakes up
   if IsSleep then
      if not table.find(SleepingPlayers, Player.UserId) then
          table.insert(SleepingPlayers, Player.UserId)

          -- // if all players are asleep
          if #SleepingPlayers == #game.Players:GetChildren() then
              -- // All players are asleep. You can enable other scripts
              ...
          else
              -- // Not all players are asleep
              ...
          end
      end
   else
      table.remove(SleepingPlayers, table.find(SleepingPlayers, Player.UserId))
   end
end)

This code is an example and can be improved in real practice

2 Likes

Just store what the players’ current actions (“states”) are on the server. Update these values whenever their states change, and check every time states change to see if your criteria is met.

@pozzzy333’s method is equally valid. Many angles to attack this issue with. You could even use attributes, or CollectionService if you needed to

3 Likes