Door endlessly opens and closes with new coding system

So, I’m making a complete game renovation on one of my older games, and with that I made some new blast doors, however the door will endlessly open and close.

My code:

local acceptedItems = {
	["SCP:F - Level 1 Keycard"] = false;
	["SCP:F - Level 2 Keycard"] = true;
	["SCP:F - Level 3 Keycard"] = true;
	["SCP:F - Level 4 Keycard"] = true;
	["SCP:F - Level 5 Keycard"] = true;
	["SCP-005"] = true;
}

local reader1 = script.Parent.KeycardReader.Button2
local reader2 = script.Parent.KeycardReader2.Button2

local MainDoor = script.DoorPart

local tr = 0
local db = false

local maxTR = 100

function reader(hit)
	if db == false then
		db = true
		
		for i, v in pairs(acceptedItems) do
			if acceptedItems[hit.Parent.Name] == true then
				repeat
					wait()
					MainDoor.Position = MainDoor.Position + Vector3.new(0, 0.1, 0)
					tr += 1
				until
				tr == maxTR

				tr = 0
				wait(3)

				repeat
					wait()
					MainDoor.Position = MainDoor.Position + Vector3.new(0, -0.1, 0)
					tr += 1
				until
				tr == maxTR
				tr = 0
			end
			wait(2)
			db = false
		end
		
		wait(.2)
		db = false
	end
end

reader1.Touched:Connect(function(touched)
	reader(touched)
end)

reader2.Touched:Connect(function(touched)
	reader(touched)
end)
1 Like

Couldn’t you just use table.find() instead? It’d be a way easier approach instead of just looping through everything (And checking for the Name of Hit.Parent)

Also I’d personally use for loops, instead of repeats

local acceptedItems = {
	--["SCP:F - Level 1 Keycard"] = false;
	"SCP:F - Level 2 Keycard";
	"SCP:F - Level 3 Keycard";
	"SCP:F - Level 4 Keycard";
	"SCP:F - Level 5 Keycard";
	"SCP-005";
}

local reader1 = script.Parent.KeycardReader.Button2
local reader2 = script.Parent.KeycardReader2.Button2
local MainDoor = script.DoorPart

local DB = false

local function Reader(Hit)
    if not DB and Hit.Parent and table.find(acceptedItems, Hit.Parent.Name) then
        DB = true

        for Loop = 0, 100 do
            wait()
            MainDoor.Position += Vector3.new(0.1, 0)
        end

        wait(3)

        for Loop = 0, 100 do
            wait()
            MainDoor.Position -= Vector3.new(0.1, 0)
        end

        wait(.2)
        DB = false
    end
end)

reader1.Touched:Connect(Touched)
reader2.Touched:Connect(Touched)
2 Likes

Since you are renovating your game, you should consider using TweenService instead of running loops to smoothly change the door’s position. Also, you can just directly attempt to index for the keycards name to check for its validity instead of finding the keycard in the table.

local acceptedItems = {
	["SCP:F - Level 1 Keycard"] = false;
	["SCP:F - Level 2 Keycard"] = true;
	["SCP:F - Level 3 Keycard"] = true;
	["SCP:F - Level 4 Keycard"] = true;
	["SCP:F - Level 5 Keycard"] = true;
	["SCP-005"] = true;
}

local reader1 = script.Parent.KeycardReader.Button2
local reader2 = script.Parent.KeycardReader2.Button2

local MainDoor = script.DoorPart

local db = false

local tween = game:GetService('TweenService')
local openDoor = tween:Create(MainDoor, TweenInfo.new(1), {CFrame = MainDoor.CFrame + Vector3.new(0, 10, 0)}) --moves the door 10 studs up in 1 second
local closeDoor = tween:Create(MainDoor, TweenInfo.new(1), {CFrame = MainDoor.CFrame}) --moves the door back down in 1 second

function reader(hit)
	if not db then
		db = true		
        if acceptedItems[hit.Parent.Name] then --if keycard doesnt exist or isnt accepted, it will return nil or false. if it does and it's accepted, it will return true.
            openDoor:Play() --plays the tween that opens the door
            wait(3)
            closeDoor:Play() --after 3 seconds, play the tween that closes the door
        end
        wait(2)
        db = false
	end
end

reader1.Touched:Connect(reader) --parameters are automatically passed to the connected function
reader2.Touched:Connect(reader)
2 Likes

Thank you! I was wondering how i could fix that!

Also, I just realized a flaw with my script. The position for the closing tween should’ve been the original position, not 10 studs below, so that when the door closes it returns to its stationary position.

local closeDoor = tween:Create(MainDoor, TweenInfo.new(1), {CFrame = MainDoor.CFrame}) --removed the vector3

Thanks! (10chars_10chars_10chars)