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

How do I make a door that opens when there are 2 people on the game?

Asked by
duckyo01 120
10 years ago

I think its like

local Door = script.Parent
function onTouch(part)
Player = game.Players:GetPlayerFromCharacter(script.Parent)
if Player ~= nil then
if Player>= 2
Door.CanCollide = false 
Door.Transparency = 0.6
if Players < 2 then
Door.CanCollide = true
Door.Transparency = 1
end
end
end
script.Parent.Touched:connect(onTouch)

And this wont work please help

2 answers

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

First: your syntax isn't right. You should tab your code, and that will make it clearer.

ifs always need thens, and you should use else when you're talking about "if this do that, otherwise do that*:

local Door = script.Parent
function onTouch(part)
    Player = game.Players:GetPlayerFromCharacter(script.Parent)
    if Player ~= nil then
        if Player>= 2 then
            Door.CanCollide = false 
            Door.Transparency = 0.6
        else
            Door.CanCollide = true
            Door.Transparency = 1
        end
    end
end
script.Parent.Touched:connect(onTouch)

In addition to that, there are a few issues.

script.Parent isn't going to be part of a player's Character -- script.Parent is a Door.

You care about the part touching the door, which is called part. At the same time, use a local variable!

    local Player = game.Players:GetPlayerFromCharacter(part.Parent)

Now, Player is a player object. It clearly won't be bigger than 2. We want to get the number of players, which, funnily is a part of the Players service and is called NumPlayers (Google things!)

    if game.Players.NumPlayers >= 2 then

At the end, the connection line is correct, but Door is script.Parent already, so you should probably use that to just make it that much clearer:

Door.Touched:connect( onTouch )
0
Thanks I'll see if it works duckyo01 120 — 9y
Ad
Log in to vote
0
Answered by 10 years ago
local Door = script.Parent
script.Parent.Touched:connect(function(part)
if game.Players.NumPlayers >= 2 then
Door.CanCollide = false 
Door.Transparency = 0.6
else
Door.CanCollide = true
Door.Transparency = 1
end
end
script.Parent.Touched:connect(onTouch)

NumPlayers will tell you how many people are in the server, to use :GetPlayerFromCharacter() you need to add game.Players:GetPlayerFromCharacter(part.Parent) because script.Parent(which is door) is obviously not a player. http://wiki.roblox.com/index.php?title=API:Class/Players/NumPlayers

Answer this question