Hi, I'm trying to make a quick script where if the number of players in the game is 1, then it would print "test". Since I can't use NumPlayers, I am confused on how to do this. I have a script here that does not work and does not show any error in output. Any help would be appreciated.
if game.Players == 1 then print("test") end
You can use a IntValue to keep track of how many players that entered your game. I'll just give you an example and this is just a standard example so better practice can be made later.
In a script:
local Players = game:GetService("Players") local AmountOfPlayers = nil Players.PlayerAdded:Connect(function(player) -- Check if our game hasn't created the AmountOfPlayers object first -- If so create our new object if not AmountOfPlayers then AmountOfPlayers = Instance.new("IntValue") AmountOfPlayers.Name = "AmountOfPlayers" AmountOfPlayers.Value = 0 AmountOfPlayers.Parent = workspace end -- If our object exists then increment the games player count if AmountOfPlayers then AmountOfPlayers.Value = AmountOfPlayers.Value + 1 end end) Players.PlayerRemoving:Connect(function(player) -- Decrease AmountOfPlayers Value by 1 each time a player is removed from the game. end) -- Then you could check in another script like local playersConnectedAmount = workspace:WaitForChild("AmountOfPlayers") if playersConnectedAmount.Value == 1 then -- Run Code ... end
while wait() do if #game:GetService("Players"):GetPlayers() == 1 then print("test") end end
If this helped you, please upvote and accept the answer.