I am trying to make a 'Door' system in which when you get close to a door and press 'E', it opens and when you press 'E' again (if it's still open) it closes.
The Problem
I'm having with this is that when there are two doors (obviously I'm going to have more than that anyway), only one door works. (They are both named different)
I've reviewed my code with no avail, so I've come here. Any help would be greatly appreciated!
Local script:
--[[ Varibles --]] local plr = game:GetService("Players").LocalPlayer local mouse = plr:GetMouse() repeat wait() until plr.character local char = plr.character local enabled = false local obj --[[ End of Varibles --]] game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.E then if enabled == true then game.ReplicatedStorage.RemoteEvent:FireServer(obj) end end end) while wait() do for i,v in pairs (game:GetService("Workspace").interactiveObjects:GetChildren()) do if (v.Position - char.HumanoidRootPart.Position).magnitude < 10 then enabled = true obj = v else if enabled == true then enabled = false obj = nil end end end end
Server Script:
local TweenService = game:GetService("TweenService") game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(p,obj) local object = game:GetService("Workspace").interactiveObjects:WaitForChild(obj.Name) if object:WaitForChild("isOpen").Value == false then local tween = TweenService:Create(object, TweenInfo.new(1,Enum.EasingStyle.Linear), {CFrame = object:WaitForChild("Open").CFrame}) tween:Play() tween.Completed:wait() object:WaitForChild("isOpen").Value = true else local tween = TweenService:Create(object, TweenInfo.new(1,Enum.EasingStyle.Linear), {CFrame = object:WaitForChild("Closed").CFrame}) tween:Play() tween.Completed:wait() object:WaitForChild("isOpen").Value = false end --wait(1) end)
How the door(s) are set up in the workspace: Photo What it does: Video