Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Cant check number of Players?

Asked by 5 years ago

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
0
if #game.Players:GetPlayers() == 1 then 522049 152 — 5y
0
^ User#19524 175 — 5y
0
ill try that thx TheLionLiar 39 — 5y
0
still same result TheLionLiar 39 — 5y
View all comments (4 more)
0
The script probably loaded first so put a wait(5) before the first line 522049 152 — 5y
0
No that is bad practice. User#19524 175 — 5y
0
doesnt work TheLionLiar 39 — 5y
0
Why can't you use NumPlayers? Azarth 3141 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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
0
wow pretty long, thx TheLionLiar 39 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
while wait() do
    if #game:GetService("Players"):GetPlayers() == 1 then
        print("test")
    end
end

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

Answer this question