Improving access card system to a room

  1. What do you want to achieve?
    Hello Robloxians, I’m getting back into coding and wanted to see if I still understood Lua. So, I made a room with an access pad on the outside. Everything works, but I was just wondering if there’s a better way of achieving the same outcome with less code or what I could add to challenge myself.

  2. What is the issue?

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible so that it’s easier for people to help you!

--Services--

local TweenService = game:GetService("TweenService")

-- Variables --

local Room = game.Workspace.Room
local cardReader= Room.CardReader
local Confirm = cardReader.StatusLights.Confirm
local Active = cardReader.StatusLights.Active
local cardTrigger = cardReader.Trigger
local Door = Room.Door
local precautionPart = Room.Precaution

local Open = false
local playerTouching = false


-- Door Changing Status--

local doorClosedPosition = Door.Position
local doorOpenPosition = Vector3.new(-14.741, 0.375, 88.125)
local doorClosedSize = Room.Door.Size
local doorOpenSize = Vector3.new(6.017, 0.25, 0.25)

-- Door Tweens--

local Opengoal = {}
Opengoal.Size = doorOpenSize
Opengoal.Position = doorOpenPosition

local Closedgoal = {}
Closedgoal.Size = doorClosedSize
Closedgoal.Position = doorClosedPosition

local tween_Info = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false)
local openTween = TweenService:Create(Door, tween_Info, Opengoal)

local closeTween = TweenService:Create(Door, tween_Info, Closedgoal)

-- Door Sounds--

local OpenSound = Door.Sounds.DoorOpen
local CloseSound = Door.Sounds.DoorClose

-- Inital Status--
local NotConfirmed = cardReader.StatusLights.Confirm.BrickColor
local NotScanned = cardReader.StatusLights.Active.BrickColor

-- Part that prevents the door from closing while player is on the door. --
precautionPart.Touched:Connect(function(hit)
	local character = hit.Parent
	if character and character:FindFirstChild("Humanoid") then
		playerTouching = true
	end
end)

precautionPart.TouchEnded:Connect(function(hit)
	local character = hit.Parent
	if character and character:FindFirstChild("Humanoid") then
		playerTouching = false
	end
end)

-- Function to check if player is touching before door closes
function isPlayerTouchingPrecaution()
	return playerTouching
end


cardTrigger.ProximityPrompt.Triggered:Connect(function()
	
	if Open == false then
		cardTrigger.ProximityPrompt.Enabled = false
		cardReader.StatusLights.Confirm.BrickColor = BrickColor.new("Lime green")
		cardReader.StatusLights.Active.BrickColor = NotConfirmed
		openTween:Play()
		OpenSound:Play()
		task.wait(1)
		cardTrigger.ProximityPrompt.Enabled = true
		cardTrigger.ProximityPrompt.ActionText = "Press to Close"
		Open = true
	elseif Open == true and isPlayerTouchingPrecaution() == false then
		cardReader.StatusLights.Confirm.BrickColor = NotConfirmed
		cardReader.StatusLights.Active.BrickColor = NotScanned
		closeTween:Play()
		CloseSound:Play()
		cardTrigger.ProximityPrompt.ActionText = "Press To Open"
		Open = false
	end

end)
1 Like

1. Change doorOpenPosition

The variable “doorOpenPosition” could be set to:

doorClosedPosition + Vector3.new(0, 1, 0) -- Or whatever axis and position

This can help if you ever move the door because it won’t tween to a random position and can just tween up.

2. Remove unnecessary function

The function “isPlayerTouchingPrecaution” seems unnecessary because it just returns 1 value. Just use the playerTouching variable.

3. Debounce

You should add a debounce on the proximityPrompt in case it is fired multiple times before it is disabled (you wouldn’t want to run the same tween 3 times in a row).

Everything else seems fine. There may be some other minor improvements but overall I don’t see anything wrong with it.

2 Likes

Instead of “if Open == false then” or “if Open == true then”, you could simply do “if not Open then” and “if Open then”. (If not also works for nil types, not just false booleans.)

2 Likes

Thank you, that is some good advice! I’ll make changes now.

Thank you for your input. I will make some changes.