I made this script that's supposed to, at random, close a door that the player is about to walk through by itself. I did this by having 4 total parts, two of which are the actual door closing parts, and the other two acting as "detectors" to see what side the player is coming through. I then put which side they're coming through in a value, which the random door closing script can read. It looks something like this:
function AutoClose(part) if part.Parent:FindFirstChild("Humanoid") then if Open == true then -- if door is already opened.. if not Debounce2 then Debounce2 = true local closechance = math.random(1, 6) if closechance == 3 then -- do stuff to close door Open = false CloseSound:Play() -- "closesound" being another sound script.Parent.Door.DoorError:Play() Door.Transparency = 0 for i,v in pairs(Door:GetChildren()) do if v:IsA("Decal") then v.Transparency = 0 end end for i = 1,(Door.Size.z / 0.20) do Door.CFrame = Door.CFrame + (Door.CFrame.lookVector * 0.20) wait() end end end wait(5) Debounce2 = false end end end -- if they're near the front... script.Parent.SCP079partFront.Touched:Connect(function(part) if script.Parent.PlayerLocation.Value == "front" then AutoClose(part) end end) -- if they're near the back... script.Parent.SCP079partBack.Touched:Connect(function(part) if script.Parent.PlayerLocation.Value == "back" then AutoClose(part) end end)
The script for sending which side they're going through looks like this:
script.Parent.front.Touched:Connect(function(part) if part.Parent:FindFirstChild("Humanoid") then script.Parent.PlayerLocation.Value = "front" end end) script.Parent.back.Touched:Connect(function(part) if part.Parent:FindFirstChild("Humanoid") then script.Parent.PlayerLocation.Value = "back" end end)
This works almost perfectly, although there's a small problem; if the player goes and opens the door, but then walks back from the same side in which they came, there's a chance the door will close even though they weren't walking towards it with the intention of going through. This is a pretty small problem, but nonetheless I was wondering if there was a better way of detecting if a player is about to walk through an open door.
In case it helps anyone familiar with SCP, I'm trying to replicate that thing SCP-079 does when he auto-closes a door you're about to walk through (in SCP: Containment Breach).