I made a script for a door that opens and closes based on a player’s magnitude to the door which works fine, however whenever I reset the previous humanoid root part is still being detected. Also, when I test the door in a server with 2+ players; when a player gets close enough to activate the door, it continuously opens and closes. What could I do to fix this?
local door = script.Parent
local finish = door.Finish
local endpart = door.Endpart
local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(
3,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false,
0
)
local goal = {CFrame = finish.CFrame}
local goal2 = {CFrame = endpart.CFrame}
local DoorStart = TweenService:Create(door,info,goal)
local DoorEnd = TweenService:Create(door,info,goal2)
local debounce = false
local opened = false
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
repeat wait() until char.Parent
local HRP = char:WaitForChild("HumanoidRootPart")
while true do
local mag = (HRP.Position - finish.Position).Magnitude
if opened == false then
if mag < 15 then
if not debounce then
debounce = true
DoorStart:Play()
print("Activated")
repeat wait() until DoorStart.PlaybackState == Enum.PlaybackState.Completed
opened = true
debounce = false
end
end
end
if opened == true then
if mag > 15 then
if not debounce then
debounce = true
DoorEnd:Play()
print("Deactivated")
repeat wait() until DoorEnd.PlaybackState == Enum.PlaybackState.Completed
opened = false
debounce = false
end
end
end
print(mag)
wait()
end
end)
end)
The problem that the old humanoidRootPart keeps getting checked could be solved by just replacing while true do with while char.Parent do. However, that wouldn’t solve the other problem. I believe this code will solve both of them, but I’m not sure, because I haven’t tested it.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local OPEN_DISTANCE = 15
local door = script.Parent
local finish = door.Finish
local endpart = door.Endpart
local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(
3,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false,
0
)
local goal = {CFrame = finish.CFrame}
local goal2 = {CFrame = endpart.CFrame}
local DoorStart = TweenService:Create(door,info,goal)
local DoorEnd = TweenService:Create(door,info,goal2)
local debounce = false
local opened = false
local charAddConns = {}
local hrps = {}
local function closeDoor()
debounce = true
DoorEnd:Play()
print("Deactivated")
DoorEnd.Completed:Wait()
opened = false
debounce = false
end
local function openDoor()
debounce = true
DoorStart:Play()
print("Activated")
DoorStart.Completed:Wait()
opened = true
debounce = false
end
local function handleDoor()
if debounce then
return
end
for _, hrp in pairs(hrps) do
local mag = (hrp.Position - finish.Position).Magnitude
if mag < OPEN_DISTANCE then
if not opened then
opendDoor()
end
return
end
end
if opened then
closeDoor()
end
end
local function onCharAdded(char)
hrps[Players:GetPlayerFromCharacter(char)] = char.HumanoidRootPart
end
local function onPlrAdded(plr)
charAddConns[plr] = plr.CharacterAdded:Connect(onCharAdded)
end
local function onPlrRemoving(plr)
charAddConns[plr]:Disconnect()
charAddConns[plr], hrps[plr] = nil, nil
end
Players.PlayerAdded:Connect(onPlrAdded)
Players.PlayerRemoving:Connect(onPlrRemoving)
RunService.Heartbeat:Connect(handleDoor)
Unfortunately, it didn’t work either. The door opens and closes continuously when it is just a 1 person server or 2+ server. I also added in a print(mag) to see if the old humanoid root part after reset would still be detected and it was still being detected. My placement of the print(mag) could be the problem but idk.
Here is a link to the model if you would like to see it.
I edited the code again. I had forgotten to remove the old humanoidrootpart from the table. Now it is removed and replaced with the new one when the player respawns.
The humanoid problem is fixed now thanks to you. I don’t know how to fix this problem of it constantly reopening and closing when someone is close enough to activate it though.
Okay, I was able to fix the problem happening when it was 1 person only, but the same problem of it opening and closing continuously is still happening in 2+ player servers. I believe it is because both the requirements for opening and closing are being met due to one player being close enough to open and one being far enough to close. When both players are close enough the door stays open, and when both are far away, it stays closed. I can’t seem to figure out how to fix that though.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local OPEN_DISTANCE = 15
local door = script.Parent
local finish = door.Finish
local endpart = door.Endpart
local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(
3,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false,
0
)
local goal = {CFrame = finish.CFrame}
local goal2 = {CFrame = endpart.CFrame}
local DoorStart = TweenService:Create(door,info,goal)
local DoorEnd = TweenService:Create(door,info,goal2)
local debounce = false
local opened = false
local charAddConns = {}
local hrps = {}
local function closeDoor()
debounce = true
DoorEnd:Play()
print("Deactivated")
DoorEnd.Completed:Wait()
opened = false
debounce = false
end
local function openDoor()
debounce = true
DoorStart:Play()
print("Activated")
DoorStart.Completed:Wait()
opened = true
debounce = false
end
local function handleDoor()
if debounce then
return
end
for _, hrp in pairs(hrps) do
local mag = (hrp.Position - finish.Position).Magnitude
if mag < OPEN_DISTANCE and not opened then
openDoor()
return
end
if mag > OPEN_DISTANCE and opened then
closeDoor()
end
print(mag)
end
end
local function onCharAdded(char)
hrps[Players:GetPlayerFromCharacter(char)] = char.HumanoidRootPart
end
local function onPlrAdded(plr)
charAddConns[plr] = plr.CharacterAdded:Connect(onCharAdded)
end
local function onPlrRemoving(plr)
charAddConns[plr]:Disconnect()
charAddConns[plr], hrps[plr] = nil, nil
end
Players.PlayerAdded:Connect(onPlrAdded)
Players.PlayerRemoving:Connect(onPlrRemoving)
RunService.Heartbeat:Connect(handleDoor)