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

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

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

✓ Accepted answer

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 )

1 comment

0

Thanks I'll see if it works

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

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