local o=2.8 local function u(o,u,A)if o<=u then return math.min(u,math.max(o,A))end return A end local function A(o,u)u=u or 0 local A=10^u return math.floor(o*A+.5)/A end local function D()local D={}local r=tick()local h=false local U=false local s=nil local p,a=nil,nil local i={}local v={}local e={}local function I(o)if o.Parent==nil then return false end return o.Parent:IsA("\084\111\111\108")or I(o.Parent)end local function V(o)if o:IsA("\066\097\115\101\080\097\114\116")or o:IsA("\068\101\099\097\108")then return not I(o)end return false end local function B(o)if o then if V(o)then e[o]=true h=true end for o,u in pairs(o:GetChildren())do B(u)end end end local function P()for o,u in pairs(e)do o.LocalTransparencyModifier=0 end e={}h=true s=nil if p then p:disconnect()p=nil end if a then a:disconnect()a=nil end for o,u in pairs(i)do u:disconnect()i[o]=nil end for o,u in pairs(v)do u:disconnect()v[o]=nil end end local function f(o)P()if p then p:disconnect()end p=o.DescendantAdded:connect(function(u)if V(u)then e[u]=true h=true elseif u:IsA("\084\111\111\108")then if i[u]then i[u]:disconnect()end i[u]=u.DescendantAdded:connect(function(o)e[o]=nil if o:IsA("\066\097\115\101\080\097\114\116")or o:IsA("\068\101\099\097\108")then o.LocalTransparencyModifier=0 end end)if v[u]then v[u]:disconnect()end v[u]=u.DescendantRemoving:connect(function(u)wait()if o and(u and u:IsDescendantOf(o))then if V(u)then e[u]=true h=true end end end)end end)if a then a:disconnect()end a=o.DescendantRemoving:connect(function(o)if e[o]then e[o]=nil o.LocalTransparencyModifier=0 end end)B(o)end function D.SetEnabled(u,o)if U~=o then U=o u:Update()end end function D.SetSubject(u,o)local A=o and(o:IsA("\072\117\109\097\110\111\105\100")and o.Parent)if A then f(A)else P()end end function D.Update(D)local p=false local a=tick()local i=workspace.CurrentCamera if i then local D=0 if not U then p=true else local U=(i.Focus.p-i.CoordinateFrame.p).magnitude D=(7-U)/5 if D<.5 then D=0 end if s then local A=D-s if not p and(D<1 and s<.95)then local D=o*(a-r)A=u(-D,D,A)end D=s+A else h=true end D=u(0,1,A(D,2))end if h or s~=D then for o,u in pairs(e)do o.LocalTransparencyModifier=D end h=false s=D end end r=a end return D end return D


local MAX_TWEEN_RATE = 2.8 -- per second

local function clamp(low, high, num)
	if low <= high then
		return math.min(high, math.max(low, num))
	end
	return num
end

local function Round(num, places)
	places = places or 0
	local decimalPivot = 10^places
	return math.floor(num * decimalPivot + 0.5) / decimalPivot
end

local function CreateTransparencyController()
	local module = {}


	local LastUpdate = tick()
	local TransparencyDirty = false
	local Enabled = false
	local LastTransparency = nil

	local DescendantAddedConn, DescendantRemovingConn = nil, nil
	local ToolDescendantAddedConns = {}
	local ToolDescendantRemovingConns = {}
	local CachedParts = {}

	local function HasToolAncestor(object)
		if object.Parent == nil then return false end
		return object.Parent:IsA('Tool') or HasToolAncestor(object.Parent)
	end

	local function IsValidPartToModify(part)
		if part:IsA('BasePart') or part:IsA('Decal') then
			return not HasToolAncestor(part)
		end
		return false
	end

	local function CachePartsRecursive(object)
		if object then
			if IsValidPartToModify(object) then
				CachedParts[object] = true
				TransparencyDirty = true
			end
			for _, child in pairs(object:GetChildren()) do
				CachePartsRecursive(child)
			end
		end
	end

	local function TeardownTransparency()
		for child, _ in pairs(CachedParts) do
			child.LocalTransparencyModifier = 0
		end
		CachedParts = {}
		TransparencyDirty = true
		LastTransparency = nil

		if DescendantAddedConn then
			DescendantAddedConn:disconnect()
			DescendantAddedConn = nil
		end
		if DescendantRemovingConn then
			DescendantRemovingConn:disconnect()
			DescendantRemovingConn = nil
		end
		for object, conn in pairs(ToolDescendantAddedConns) do
			conn:disconnect()
			ToolDescendantAddedConns[object] = nil
		end
		for object, conn in pairs(ToolDescendantRemovingConns) do
			conn:disconnect()
			ToolDescendantRemovingConns[object] = nil
		end
	end

	local function SetupTransparency(character)
		TeardownTransparency()

		if DescendantAddedConn then DescendantAddedConn:disconnect() end
		DescendantAddedConn = character.DescendantAdded:connect(function(object)
			-- This is a part we want to invisify
			if IsValidPartToModify(object) then
				CachedParts[object] = true
				TransparencyDirty = true
			-- There is now a tool under the character
			elseif object:IsA('Tool') then
				if ToolDescendantAddedConns[object] then ToolDescendantAddedConns[object]:disconnect() end
				ToolDescendantAddedConns[object] = object.DescendantAdded:connect(function(toolChild)
					CachedParts[toolChild] = nil
					if toolChild:IsA('BasePart') or toolChild:IsA('Decal') then
						-- Reset the transparency
						toolChild.LocalTransparencyModifier = 0
					end
				end)
				if ToolDescendantRemovingConns[object] then ToolDescendantRemovingConns[object]:disconnect() end
				ToolDescendantRemovingConns[object] = object.DescendantRemoving:connect(function(formerToolChild)
					wait() -- wait for new parent
					if character and formerToolChild and formerToolChild:IsDescendantOf(character) then
						if IsValidPartToModify(formerToolChild) then
							CachedParts[formerToolChild] = true
							TransparencyDirty = true
						end
					end
				end)
			end
		end)
		if DescendantRemovingConn then DescendantRemovingConn:disconnect() end
		DescendantRemovingConn = character.DescendantRemoving:connect(function(object)
			if CachedParts[object] then
				CachedParts[object] = nil
				-- Reset the transparency
				object.LocalTransparencyModifier = 0
			end
		end)
		CachePartsRecursive(character)
	end


	function module:SetEnabled(newState)
		if Enabled ~= newState then
			Enabled = newState
			self:Update()
		end
	end

	function module:SetSubject(subject)
		local character = subject and subject:IsA('Humanoid') and subject.Parent
		if character then
			SetupTransparency(character)
		else
			TeardownTransparency()
		end
	end

	function module:Update()
		local instant = false
		local now = tick()
		local currentCamera = workspace.CurrentCamera

		if currentCamera then
			local transparency = 0
			if not Enabled then
				instant = true
			else
				local distance = (currentCamera.Focus.p - currentCamera.CoordinateFrame.p).magnitude
				transparency = (7 - distance) / 5
				if transparency < 0.5 then
					transparency = 0
				end

				if LastTransparency then
					local deltaTransparency = transparency - LastTransparency
					-- Don't tween transparency if it is instant or your character was fully invisible last frame
					if not instant and transparency < 1 and LastTransparency < 0.95 then
						local maxDelta = MAX_TWEEN_RATE * (now - LastUpdate)
						deltaTransparency = clamp(-maxDelta, maxDelta, deltaTransparency)
					end
					transparency = LastTransparency + deltaTransparency
				else
					TransparencyDirty = true
				end

				transparency = clamp(0, 1, Round(transparency, 2))
			end

			if TransparencyDirty or LastTransparency ~= transparency then
				for child, _ in pairs(CachedParts) do
					child.LocalTransparencyModifier = transparency
				end
				TransparencyDirty = false
				LastTransparency = transparency
			end
		end
		LastUpdate = now
	end

	return module
end

return CreateTransparencyController
