Door Script Review

Hey!
I made a door script that animates a door when people get close, basic, but for some reason was hard for me.
It is buggy, I know that, not sure why, but I hope someone does know why.

local Door = script.Parent
local Range = Door.Range
local Main = Door.Main
local Closed = Door.Closed
local MoveToPart = Door.MoveToPart
local TweenService = game:GetService("TweenService")
local Opened = false
local InRange = {}

local DoorMove = TweenInfo.new(
	1,
	Enum.EasingStyle.Cubic,
	Enum.EasingDirection.Out,
	0, 
	false
)

local Open = {
	Position = Vector3.new(0,0,0) + MoveToPart.Position
}

local OpenTween = TweenService:Create(Main, DoorMove, Open)

local Close = {
	Position = Vector3.new(0,0,0) + Closed.Position
}

local CloseTween = TweenService:Create(Main, DoorMove, Close)

Range.Touched:Connect(function(hit)
	local FindHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if FindHumanoid then
		local RemoveTable = table.insert(InRange, FindHumanoid)
	end
	if FindHumanoid and Opened == false then
		Opened = true
		OpenTween:Play()
	end
end)

Range.TouchEnded:Connect(function(hit)
	local FindHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if FindHumanoid then
		local FindTableSpot = table.find(InRange, FindHumanoid)
		local RemoveTable = table.remove(InRange, FindTableSpot)
	end
	if FindHumanoid and Opened == true and #InRange == 0 then
		Opened = false
		CloseTween:Play()
	end
end)
1 Like