Issues with Press E to open Doors

Hello, just like the title says, I’ve been working on a code that would open the closest door to the player if he presses E (First Enabling a GUI that says “Press E to interact”, then waiting for user input E key to fire a remote event, check the magnitude again and fire back to all clients to tween the door).

My issue as of now is that it semi-breaks when there is more than 1 door, the GUI being set from false to true, same with the function calling. I will attach the code below:

Local Script as child of a GUI in StarterGui:

local UIS = game:GetService("UserInputService")
local Doors = game:GetService("Workspace"):FindFirstChild("Doors")
local Interaction_Gui = script.Parent
local DoorEvent = game.ReplicatedStorage.RemoteEvents.DoorEvent
local TS = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(1.3, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, true)
local tweeninfo2 = TweenInfo.new(2.3, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, true)
local function Interaction(door)
local connection = nil
	

connection = UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.E and not gameProcessed then
			local Open_bool = door.Open.Value
			if not Open_bool then
			DoorEvent:FireServer(door)
			Interaction_Gui.Enabled = false
			print(door.Name)
			connection:Disconnect()
				end
			end
		end
	end)				
end

game:GetService("RunService").Heartbeat:Connect(function()
	for _,Interactable in pairs(Doors:GetChildren()) do
 		local Magnitude = (Interactable.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
		
		if Magnitude <= 10 and Interaction_Gui.Enabled == false then
			Interaction_Gui.Enabled = true
			Interaction(Interactable)
			print("called interaction function")
		elseif Magnitude > 10 and Interaction_Gui.Enabled == true then
			Interaction_Gui.Enabled = false
			print("False")
		end
	end
end)

DoorEvent.OnClientEvent:Connect(function(Door)
	if Door.Name == "Door" then
	local goal = {CFrame = (Door.CFrame * CFrame.new(-6.3,0,0))}
	local TweenOpen = TS:Create(Door, tweeninfo, goal)
	TweenOpen:Play()
	print("opening door")
	elseif Door.Name == "Big Door" then
	local goal = {CFrame = (Door.CFrame * CFrame.new(-8.3,0,0))}
	local TweenOpen = TS:Create(Door, tweeninfo2, goal)
	TweenOpen:Play()
	end
end)

for more then 1 doors, you have to check the last door’s position with the current door’s position, to see which is further, if it closer, it should get the treatment of the E Gui

I’m not sure how I could implement that in my script. You suggest I have a closest variable that would choose which door gets the Magnitude?

1 Like

it’s not that hard like you think, just implement close door variable

local closetdoor
 RunService.Hearbeat:Connect(function()
  for _,door in pairs(Doors:GetChildren()) do
   local Magnitude = plr:DistanceFromCharacter(door.Position)
   if Magnitude <= 10 then
    if closetdoor and plr:DistanceFromCharacter(door.Position) < plr:DistanceFromCharacter(closetdoor.Position) then
    -- The new closet door 
     closetdoor = door
   else
    -- ignore the door, doesnt need the else but you do you
   end
1 Like

Oh, I get it now. Thanks a lot!

1 Like