Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Cant check number of Players?

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

8 comments

0

if #game.Players:GetPlayers() == 1 then

0

^

0

ill try that thx

0

still same result

0

The script probably loaded first so put a wait(5) before the first line

0

No that is bad practice.

0

doesnt work

0

Why can't you use NumPlayers?

2 answers

✓ Accepted answer

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

1 comment

0

wow pretty long, thx

while wait() do
	if #game:GetService("Players"):GetPlayers() == 1 then
		print("test")
	end
end

If this helped you, please upvote and accept the answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.