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)