Employee Door System

I created this door system for employee doors where the employee uses a proximityprompt to open the door. It opens if the player is in the correct group and correct rank in that group. I’m not the best at scripting but this code does work just wondering if this can be improved and if there is a way to have it just as one script instead of having the script in each card reader. Like is there a way to have one script for all the doors in the game and will open the door being used? I tried doing this by having one script for each door model but ran into a problem with the both card readers changing colors and not the card reader being used.

This is the script that is placed in the card reader which has the proximityprompt keep in mind there is two card readers for the door. So this script is placed in both.

local doors = {
	script.Parent.Parent.Door1,
	script.Parent.Parent.Door2
}

local CardReader = script.Parent
local Light = CardReader

local ProximityPrompt = CardReader:FindFirstChild("ProximityPrompt")

local deniedSound = script.Parent.AccessDenied
local grantedSound = script.Parent.AccessGranted


local groupId = game.ServerStorage.GroupId.Value
local requiredRank = game.ServerStorage.GroupRank.Value

local unlockDuration = 2
local cooldownDuration = 2

local cardReaderModule = require(game.ServerScriptService.cardReaderCooldown)


local function unlockDoor()
	cardReaderModule.sharedValue = false
	for _, door in ipairs(doors) do
		door.CanCollide = false
	end
	grantedSound:Play()
	Light.BrickColor = BrickColor.new("Lime green")

	wait(unlockDuration)

	for _, door in ipairs(doors) do
		door.CanCollide = true
	end
	Light.BrickColor = BrickColor.new("Bright red")
	wait(cooldownDuration)
	cardReaderModule.sharedValue = true
end

local function denyAccess()
	cardReaderModule.sharedValue = false
	deniedSound:Play()
	
	for i = 1, 4 do
		Light.BrickColor = BrickColor.new("Bright red")
		wait(0.2)
		Light.BrickColor = BrickColor.new("Persimmon")
		wait(0.2)
	end

	Light.BrickColor = BrickColor.new("Bright red")
	wait(cooldownDuration)
	cardReaderModule.sharedValue = true
end

local function onPromptTriggered(player)
	
	print("Proximity prompt triggered by:", player.Name)
	
	local canUseCardReader = cardReaderModule.sharedValue
	
	if not canUseCardReader then
		print("Cooldown")
		return
	end
	
	if player:IsInGroup(groupId) and player:GetRankInGroup(groupId) >= requiredRank then
		unlockDoor()
		print("Granted")
	else
		denyAccess()
		print("Denied")
	end
end

ProximityPrompt.Triggered:Connect(onPromptTriggered)

I also created a module script to communicate between the scripts if the card reader is on cooldown so the players can’t spam the proximityprompt.

local sharedModule = {}

sharedModule.sharedValue = true

return sharedModule

I would love any feedback or solutions to make this one script for either the door model or for all the door models in the game.

Thanks,
Mystical_Bizarre

Consider using CollectionService and a bit of Roblox OOP.

3 Likes

Didn’t think of that, I’ll look into that and try using that.

Depending on your goal with these doors I would throw the doors into a dictionary with whatever being the dict name and define a type for each door so for example: Time it will be open for, what employees will be able to access it, access granted/denied color3 instances and whatever else you would need.

I also would instead of enableing or disabling the collision of the door to instead tween it left or right to make it look nicer as setting collision is kind of outdated…

Apart for that it looks alright. OOP would be nice but yeah.