Door opening and closing

I made three doors that open when you press E which are tagged by CollectionService. When I open and close one it will open/close normally, but when you open one, then go to another one and open it, it does the close tween. I know it is because it treats it as the same door but I do not know what to do to fix it.

Localscript in a Billboardgui in StarterGui :

local CollectionService = game:GetService("CollectionService")
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local DoorHandle = ReplicatedStorage:WaitForChild("DoorHandle")
local gui = script.Parent
local currentCloseDoor = nil
local doorTable = CollectionService:GetTagged("DoorRootPart")
local requiredDistance = 10
local debounce = true

local function doorCall(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		DoorHandle:FireServer(currentCloseDoor)
		print("fired server"..currentCloseDoor.Name)
	end
end

local function findclosestDoor()
	local brick = nil
	local minDist = 10
	for i, v in pairs(doorTable) do
		local distance = player:DistanceFromCharacter(v.Position)
		if (distance < requiredDistance and distance < minDist) then
			brick = v
			minDist = distance
		end
	end
	return brick
end


local function popup()
	for _, door in pairs(doorTable) do
		if player:DistanceFromCharacter(door.Position) < requiredDistance and findclosestDoor() == door then
			if debounce == true and #door:GetChildren() < 1 then
				debounce = false
				local guiclone = gui:Clone()
				guiclone.Name = "guiclone"
				guiclone.Parent = door 
				guiclone.TextLabel.Visible = true
				ContextActionService:BindAction("OpenDoor",doorCall,true,Enum.KeyCode.E)
				currentCloseDoor = door
				debounce = true
			end
		else
			if debounce == true then
				debounce = false
				if #door:GetChildren() > 0 then
					door.guiclone:remove()
					ContextActionService:UnbindAction("OpenDoor")					
				end
				debounce = true
			end
		end
	end
end

while wait() do
	popup()
end

Script in ServerScriptService :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DoorHandle = ReplicatedStorage:WaitForChild("DoorHandle")
local TweenService = game:GetService("TweenService")
local debounce = false
local toggle = false
local distance = 10

DoorHandle.OnServerEvent:Connect(function(player, currentCloseDoor)
	local PanelSwingInfo = TweenInfo.new()
	local PanelRoot = currentCloseDoor.Parent.Hinge
	local PanelSwingTween = TweenService:Create(PanelRoot, PanelSwingInfo, {
		CFrame = PanelRoot.CFrame * CFrame.Angles(0, math.rad(120), 0)
	})
	local PanelSwingTween2 = TweenService:Create(PanelRoot, PanelSwingInfo, {
		CFrame = PanelRoot.CFrame * CFrame.Angles(0, math.rad(-120), 0)
	})
	if debounce then
		return
	end
	if player:DistanceFromCharacter(currentCloseDoor.Position) < distance then
		if toggle == false then
			debounce = true
			PanelSwingTween:Play()
			print("Opened")
			PanelSwingTween.Completed:Wait()
			toggle = true
			debounce = false
		else
			debounce = true
			PanelSwingTween2:Play()
			print("Closed")
			PanelSwingTween2.Completed:Wait()
			toggle = false
			debounce = false
		end
	end
end)
1 Like

do something for the individial door like debounce[door] = true, or the slightly less efficient way (but a lot easier) would be to create an object inside of the door after you open it and call it “NeedsToBeClosedNow”, and then close the door if it finds door:FindFirstChild(“NeedsToBeClosedNow”)

Add states for each door that declares if the door is open or closed, then code your script according to each door, not to the player.

Change the names. Having the same name is bad.

I ended up adding a boolvalue in each door and checked if it was true or false, and it ended up working.Do you know if doing that is something that is safe against exploits or would your method be better?

You would probably want a server script checking the bool values. Exploiters can change anything that they have access to. (Workspace instances, local scripts, replicatedStorage instances etc)