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

How can I make a door that only opens when there is a certain amount of players in the server?

Asked by 4 years ago

I am trying to make a door that opens once there is a certain amount of players in the server. I have a script but it works no matter how many players are in the server

Heres the script:

local Players = game:GetService("Players")

if #Players:GetPlayers(2) then
local door = script.parent
do
wait (40)

    door.CanCollide = false

    door.Transparency = 1
end
end

This is a Script that is added into a part

3 answers

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

Close but you should put anything in the brackets of line 3 because it will return a table and you should have the script listen for the number of players to change otherwise it will only read your code once...

local Players = game:GetService("Players")
local door = script.parent

Players.PlayerAdded:Connect(function() --everytime a player joins...
    if #Players:GetPlayers() >= 2 and door.CanCollide == true then --if there are more than two players and the door isnt open, open the door
        wait(40) 
        door.CanCollide = false
        door.Transparency = 1
    end
end)

If you're using this method it may also be useful to listen for players leaving...

local Players = game:GetService("Players")
local door = script.parent

local function checkPlayers() --check if the door should be open and act upon it
    if #Players:GetPlayers() >= 2 and door.CanCollide == true then --if there are more than two players and the door isnt open, open the door
        wait(40) 
        door.CanCollide = false
        door.Transparency = 1
    elseif #Players:GetPlayers() < 2 and door.CanCollide == false then --if there arent enough players and the door is open, close the door
        door.CanCollide = true
        door.Transparency = 0
    end
end

Players.PlayerAdded:Connect(checkPlayers) --everytime a player joins...
Players.PlayerRemoving:Connect(checkPlayers) --everytime a player leaves...

Hopefully this works...

0
game.Players.PlayerAdded:Connect(function() Corrupt_Wolfstrik3r 51 — 4y
0
I have "Players" stored as a variable "local Players = game:GetService("Players")" .. why should I use game.Players.PlayerAdded instead of game:GetService("Players").PlayerAdded ?? AlexTheCreator 461 — 4y
Ad
Log in to vote
0
Answered by
SirGamezy 186
4 years ago

Try this:

local Players = game:GetService("Players")
local door = script.Parent

while true do
    if #Players:GetPlayers() == 2 then
        door.CanCollide = false
        door.Transparency = 1
    end
    wait(.01)
end

We use a while loop so the number of players is always updated. If the number of players is 2, then the door will open. Otherwise, it will remain closed.

Remember to add a wait() when using while true loops, because this prevents the client from crashing.

When you use the :GetPlayers function, it returns a table, so there should be nothing in the brackets.

0
No disputing the answer but out of interest does running lots of "while true do" loops in a game slow performace? You'd think just cheking for a change would be more effective? AlexTheCreator 461 — 4y
Log in to vote
0
Answered by 4 years ago
local Players = 0


game:GetService("RunService").Heartbeat:Connect(function()

    if Players >=2 then
    script.Parent.Transparency = 1
        script.Parent.CanCollide = false
    elseif Players <2 then
        script.Parent.Transparency = 0
        script.Parent.CanCollide = true
    end

end)








game.Players.PlayerAdded:Connect(function()
    Players = Players + 1
end)
game.Players.PlayerRemoving:Connect(function()
    Players = Players - 1
end)

try this here i tested it and it seems to work

0
you could just count the number of players from within the heartbeat function, you dont need those other listeners :) "if #game.Players:GetChildren() >= ..." AlexTheCreator 461 — 4y

Answer this question