Hey! How would i made my door open and stay open when a humanoid or a char is touching it?
this is my script now
local door = script.Parent:WaitForChild("Door")
local sensor1 = script.Parent:WaitForChild("Sensor1")
local sensor2 = script.Parent:WaitForChild("Sensor2")
local noise = door:FindFirstChild("Noise")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1.79, Enum.EasingStyle.Linear, Enum.EasingDirection.In,0,false,0)
local doorPos1 = {CFrame = CFrame.new(-68.683, 10.023, -53.5)}
local doorPos2 = {CFrame = CFrame.new(-68.683, 3.25, -53.5)}
local open = tweenService:Create(door,tweenInfo,doorPos1)
local close = tweenService:Create(door,tweenInfo,doorPos2)
local db = false
sensor1.Touched:Connect(function()
if db == false then
db = true
open:Play()
wait(3.3)
close:Play()
wait(3.3)
db = false
end
end)
sensor2.Touched:Connect(function()
if db == false then
db = true
open:Play()
wait(3.3)
close:Play()
wait(3.3)
db = false
end
end)
Using a Touched event for this may not be the best way.
How about checking if the Humanoid is between Sensor Position (sensor ± 4 studs, sensor +_ 4 studs, sensor +_ 4 studs), so basically within an 8 stud cube of the sensor’s Position and if they are keep the door open.
I would recommend against using the Touched event (for this instance)
Instead, you could do something else.
Choose a distance that the player must reach between them and the door in order for it to open/close.
Simply get the distance from the sensor and each character in the game, using a function similar to this:
--pass the sensor through this function
function FindClosestPlayer(part)
if (not part) then return end
local lowest = math.huge
for _,v in ipairs(game:GetService("Players")) do
local character = v.Character
if (character) then
local hrp = character.PrimaryPart
if (hrp) then
local distance = (hrp.Position - part.Position).magnitude
if (distance < lowest) then
lowest = distance
end
end
end
end
return lowest
end
Check the distance every .1 to .2 seconds in a while loop in order to determine if you should open or close the door! Something like so
local closed = true --starts out closed
while wait(.1) do
local lowest = FindClosestPlayer(sensor1)
if (lowest <= 4) then
if closed then
--open door with tween
closed = false
end
else
if (not closed) then
--close door with tween
closed = true
end
end
end
You can use Region3 to detect if there is a player there. Do this
local function Check()
local Door = nil -- Replace nil with a door instance
local MinRegion = Door.Position - (Door.Size / 2)
local MaxRegion = Door.Position + (Door.Size / 2)
local DoorRegion = Region3.new(MinRegion, MaxRegion)
local Whitelist = {} -- The whitelist of parts can be found by Region3, we will make it only detects
player.
for _, Player in pairs(game.Players:GetPlayers()) do
if Player.Character then
table.insert(Whitelist, Player.Character.PrimaryPart)
end
end
local TouchingPart = workspace:FindPartsInRegion3WithWhiteList(DoorRegion, Whitelist)
if #TouchingPart == 0 then
print("No one touched the door, close it")
else
print("Someone touching, open the door")
end
end
You should run it in a loop like check it every 1 seconds.
.TouchEnded will some time tricky and may fail if there are more than 1 player. The best way is to get all the parts using Region3 but with Whitelist. After that check if there are more than 0 part inside the Region. If equal 0 mean no one touched, we will close the door. Otherwise, we will open it.
You can make it bigger which cover the door, name it MainSensor and use it, make sure it’s invisible. Then keep the old Sensors but make it as a decoration.
local function Check()
local Door = script.Parent:WaitForChild('Sensor1')
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1.79, Enum.EasingStyle.Linear, Enum.EasingDirection.In,0,false,0)
local doorPos1 = {CFrame = CFrame.new(-68.683, 10.023, -53.5)}
local doorPos2 = {CFrame = CFrame.new(-68.683, 3.25, -53.5)}
local MinRegion = Door.Position - (Door.Size / 2)
local MaxRegion = Door.Position + (Door.Size / 2)
local open = tweenService:Create(Door,tweenInfo,doorPos1)
local close = tweenService:Create(Door,tweenInfo,doorPos2)
local DoorRegion = Region3.new(MinRegion, MaxRegion)
local Whitelist = {Door}
for _, Player in pairs(game.Players:GetPlayers()) do
if Player.Character then
table.insert(Whitelist, Player.Character.PrimaryPart)
end
end
while wait(1) do
Check()
end
local TouchingPart = workspace:FindPartsInRegion3WithWhiteList(DoorRegion, Whitelist)
if #TouchingPart == 0 then
close:Play()
else
open:Play()
end
end
Thats what im using i dont usually use region3 so idk how it works
You are running the loop inside the function and not calling it, can you please use this instead?
local function Check()
local Door = nil -- Replace nil with a door instance
local MinRegion = Door.Position - (Door.Size / 2)
local MaxRegion = Door.Position + (Door.Size / 2)
local DoorRegion = Region3.new(MinRegion, MaxRegion)
local Whitelist = {} -- The whitelist of parts can be found by Region3, we will make it only detects player.
for _, Player in pairs(game.Players:GetPlayers()) do
if Player.Character then
table.insert(Whitelist, Player.Character.PrimaryPart)
end
end
local TouchingPart = workspace:FindPartsInRegion3WithWhiteList(DoorRegion, Whitelist)
if #TouchingPart == 0 then
print("No one touched the door, close it")
else
print("Someone touching, open the door")
end
end
while wait(1) do
Check()
end