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

How to make keycard door detect the keycard every time I press E?

I have made a keycard door, but there is a problem. It only needs the player to have a keycard when opening the door once, and it lets the player open the door without a keycard after. Is there a way I can make it so every time the player needs a keycard to open the door? Event firing script: (localscript)

local runservice = game:GetService("RunService")
local desiredInterval = 0.65
local counter = 0
local holdingkey = false
local UIS = game:GetService("UserInputService")
local door = game.Workspace.ButtonDoor5
local PrimaryPart = door.PrimeDoor.PrimaryPart
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local char = player.Character
local root = char.HumanoidRootPart
local event = game.ReplicatedStorage.OpenKeyDoor
local NearDoor = false



runservice.Heartbeat:Connect(function(step)
	counter = counter + step  --step is time in seconds since the previous frame
	if counter >= desiredInterval then
		counter = counter - desiredInterval
		if char:FindFirstChild("card") then
			holdingkey = true
			if holdingkey == true then
				if (PrimaryPart.Position - root.Position).Magnitude <= 5 then
					NearDoor = true
					if (PrimaryPart.Position - root.Position).Magnitude >= 5 then
						NearDoor = false
						if mouse.Target == "PrimaryPart" then
							PrimaryPart.EToOpen.Enabled = true
						end
					end					
				end
			end
		end
	end
end)


UIS.InputBegan:Connect(function(Input)
	if Input.UserInputType == 	Enum.UserInputType.Keyboard then
		local Key = Input.KeyCode
		if Key == Enum.KeyCode.E and holdingkey and NearDoor == true then
			event:FireServer()
		end
	end
end)

Door Opening Script: (server Script inside door)

local Door1 = script.Parent.Door1
local Door2 = script.Parent.Door2
local player = game.Players.LocalPlayer
local neon = script.Parent.Button.Neon
local neonb = script.Parent.Button2.Neon2
local debounce = false
local opened = false -- new variable
local TS = game:GetService("TweenService")
local TI = TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local DoorOpen1 = {CFrame = Door1.CFrame + Door1.CFrame.lookVector * -4}
local DoorOpen2 = {CFrame = Door2.CFrame + Door2.CFrame.lookVector * 4}
local DoorClose1 = {CFrame = Door1.CFrame }
local DoorClose2 = {CFrame = Door2.CFrame }
local open1 = TS:Create(Door1, TI, DoorOpen1)
local close1 = TS:Create(Door1, TI, DoorClose1)
local open2 = TS:Create(Door2, TI, DoorOpen2)
local close2 = TS:Create(Door2, TI, DoorClose2)
local sound = script.Parent["BMRF Sliding Door Open"]
local closesound = script.Parent["BMRF Sliding Door Close"]


game.ReplicatedStorage.OpenKeyDoor.OnServerEvent:Connect(function()
		if opened == false then -- is it OPEN...?
			if debounce == false then -- then check debounce
				debounce = true
				opened = true
				neon.BrickColor = BrickColor.new("Bright green")
				neonb.BrickColor = BrickColor.new("Bright green")
				sound:Play()
				open1:Play()
				open2:Play()
				wait(1) -- wait a bit (optional)
				debounce = false -- reset the debounce
			end
		elseif opened == true then
			if debounce == false then -- same thing as above
				debounce = true
				opened = false
				neon.BrickColor = BrickColor.new("Really red")
				neonb.BrickColor = BrickColor.new("Really red")
				closesound:Play()
				close1:Play()
				close2:Play()
				wait(1)
				debounce = false
			end
		end
	end)

1 answer

✓ Accepted answer

WELL WORRY NO MORE, I made this just for you.

Here's how to make it

Add a remote event to replicated storage and call it OpenDoor

Add in starter GUI a screen GUI and add in a billboard GUI, Then put the Adornee as MainDoor, You actually have to call the main part of the door MainDoor, Or else it won't work, Add in a TextButton or image button, Add in a local script and use this code

local userInputSevice = game:GetService("UserInputService")
local door = workspace:FindFirstChild("KeycardDoor"):FindFirstChild("MainDoor")
local openDoor = game:GetService("ReplicatedStorage"):FindFirstChild("OpenDoor")

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local debounce = false

userInputSevice.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		local magnitude = (door.Position - char:WaitForChild("HumanoidRootPart").Position).magnitude
		if magnitude <= script.Parent.Parent.MaxDistance then
			if player.Backpack:FindFirstChild("Keycard") or char:FindFirstChild("Keycard") then
				if debounce == false then
					debounce = true
					openDoor:FireServer()
					print("Opened door!")
					
					wait(3)
					
					debounce = false
				end

			end
		end
	end
end)

script.Parent.MouseButton1Click:Connect(function()
	if player.Backpack:FindFirstChild("Keycard") or char:FindFirstChild("Keycard") then
		if debounce == false then
			debounce = true
			openDoor:FireServer()
			print("Opened door!")

			wait(3)

			debounce = false
		end
	end
end)

in ServerScriptService add in a script and use this

local event = game:GetService("ReplicatedStorage"):FindFirstChild("OpenDoor")
local door = workspace:FindFirstChild("KeycardDoor"):FindFirstChild("MainDoor")

event.OnServerEvent:Connect(function()
	local tweenService = game:GetService("TweenService")

	local open1Tween = tweenService:Create(door, TweenInfo.new(1,Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0), {Position = Vector3.new(-110.5, 15, -117.5), Orientation = Vector3.new(0, 90, 0)})
	local open2Tween = tweenService:Create(door, TweenInfo.new(1,Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0), {Position = Vector3.new(-114.5, 15, -121.5), Orientation = Vector3.new(0, 0, 0)})
	
	open1Tween:Play()
	wait(2)
	open2Tween:Play()
	
end)

Please inspect the code and I trust you to know what to change, Enjoy your door

4 comments

0

Would upvote if I could ngl

0

If you accept my answer, That would be nice too!

0

Thank you so much!

0

No problem

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