PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Doors that Restrict Players

Doors that Restrict Players

Jun 23 2018, 2:02 AM PST

Problem

You want to restrict access from a door and allow only specified players in.

Solution

Use CanCollide in conjunction with an exception table.

local allowed, door = {Player = true, Camoy = true}, Workspace.restrictedDoor
door.Touched:connect(function(hit)
	if game.Players:FindFirstChild(hit.Parent.Name) and allowed[hit.Parent.Name] then
		door.CanCollide = false
		wait(1.5)
		door.CanCollide = true
	end
end)

Discussion

We first define our allowance array, then the door in which we want to use as our restricted access door. We connect the Touched event to the door and check to see if what hit the door is a player, and that they’re allowed inside.

We use a new BasePart/CanCollide|CanCollide property in order to set the ability of whether you can walk through the part or not. We then wait 1½ seconds and switch it back on. You want to make sure that the amount you wait is a low number. Say for example an allowed person enters the door and right behind them comes a not allowed person. They will be able to get through because the door has not become solid yet.

The reason we don’t need debounce here is because the action can be applied 20 times in a row and the door will act correctly. If we set BasePart/CanCollide|CanCollide to false twice, the first register will just make it true after its 1½ seconds is up, and the other will do the same only a little later.

Tags:
  • vip