-
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. -
What is the issue?
-
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)