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

6 comments
This code needs to be restructured. There is no need to check the distance with a while loop.
I thought so, but I wouldn't know how right now
Oh, and since you are having trouble with multiple doors not firing (only one will) i assume this has to do with the fact that they have the same name? (please denote if this is not an issue) though why is the local obj nil? i dont see any point in the code that changes that value?
no, they're different names (I displayed that in the photo) and it's set to nil as a placeholder
You can just do 'local obj' rather than 'local obj = nil'. The former functions like the latter.
okay, done.