Door not working

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)
1 Like

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)

Workspace.Model.Part.Script:61: Expected identifier when parsing expression, got ‘[’

This error came from this line

local hrps[char] = char.HumanoidRootPart

The word local didn’t belong there, I had forgotten to remove it. I removed it now.

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.

RBLX.script RBLXoutput

Here is a link to the model if you would like to see it.

https://www.roblox.com/library/5898183160/Door

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.

1 Like

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)

I edited the code again. Does it now work?

Yes it works perfectly now, you made a slight typo in the script with opendDoor() instead of openDoor() but once it was fixed it worked.