Hello there,
I’m trying to make a clickable door. I got a door and a double door.
But the problem is, whenever I click the double door, the other door open. Here is the example
Why did this happen?
Btw here is the code:
Handler:
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local handler = {}
handler.__index = handler
function handler:create(model)
local door = setmetatable(handler, {})
door.Model = model
if model.Name == "DoubleDoor" then
door.Type = "Double"
else
door.Type = "Single"
end
if door.Model:FindFirstChild("InvisibleWall") then
door.Fake = true
else
door.Fake = false
end
door.Moving = false
door.Opening = false
door.OpenAngle = 105
-- add humanoid
if door.Type == "Double" then
local leftHumanoid = Instance.new("Humanoid")
leftHumanoid.Parent = model.LeftDoor
leftHumanoid.Touched:Connect(function(part)
handler:botOpen(part)
end)
local rightHumanoid = Instance.new("Humanoid")
rightHumanoid.Parent = model.RightDoor
rightHumanoid.Touched:Connect(function(part)
handler:botOpen(part)
end)
else
local humanoid = Instance.new("Humanoid")
humanoid.Parent = model
humanoid.Touched:Connect(function(part)
handler:botOpen(part)
end)
end
-- add click detector for player interaction
local cursorId = "rbxassetid://"..7993914509
if door.Type == "Double" then
local leftClickDetector = Instance.new("ClickDetector")
leftClickDetector.CursorIcon = cursorId
leftClickDetector.Parent = model.LeftDoor.Frame
leftClickDetector.MouseClick:Connect(function()
handler:interact()
end)
local rightClickDetector = Instance.new("ClickDetector")
rightClickDetector.Name = ""
rightClickDetector.CursorIcon = cursorId
rightClickDetector.Parent = model.RightDoor.Frame
rightClickDetector.MouseClick:Connect(function()
handler:interact()
end)
else
local clickDetector = Instance.new("ClickDetector")
clickDetector.CursorIcon = cursorId
clickDetector.Parent = model.Frame
clickDetector.MouseClick:Connect(function()
handler:interact()
end)
end
end
function handler:interact()
if not self.Moving then
if self.Opened == false then
self:open(1, false)
self.Opened = true
if self.Fake then
local sound = self.Model.InvisibleWall:FindFirstChildOfClass("Sound")
if sound then
sound:Play()
end
self:open(-1, true)
self.Opened = false
end
else
self:open(-1, true)
self.Opened = false
end
end
end
function handler:botOpen()
end
function handler:open(direction, open)
self.Moving = true
if self.Type == "Double" then
spawn(function()
self:animate(self.Model.LeftDoor, direction, open)
end)
self:animate(self.Model.RightDoor, direction * -1, open)
else
self:animate(self.Model, direction, open)
end
self.Moving = false
end
function handler:animate(doorModel, direction, open)
local doorInfo = TweenInfo.new(
0.5,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out,
0,
false,
0
)
doorModel.PrimaryPart = doorModel.Hinge
local cframe = doorModel.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(self.OpenAngle * direction), 0)
local doorTween = TweenService:Create(doorModel.Hinge, doorInfo, {CFrame = cframe})
--[[local sound = doorModel.Frame:FindFirstChildOfClass("Sound")
if sound then
sound:Play()
else
Audio.playAudioInPart(script.creak, doorModel.Frame)
end]]
doorTween:Play()
if doorModel:FindFirstChild("Glass") then
doorModel.Glass.CanCollide = false
end
doorModel.Frame.CanCollide = false
doorTween.Completed:Wait()
doorModel.Frame.CanCollide = not open
if doorModel:FindFirstChild("Glass") then
doorModel.Glass.CanCollide = open
end
end
return handler