local w=2.8 local function i(w,i,Y)return Y>i and i or Y<w and w or Y end local Y=math.floor local function W(w,i)local W=10^i return Y(w*W+.5)/W end local function o()local Y={}local o=tick()local v=false local k=false local C=nil local I,c=nil,nil local u={}local T={}local f={}local function Q(w)if w.Parent==nil then return false end return w.Parent:IsA("\084\111\111\108")or Q(w.Parent)end local function V(w)if w:IsA("\066\097\115\101\080\097\114\116")or w:IsA("\068\101\099\097\108")then return not Q(w)end return false end local function S(w)if w then if V(w)then f[w]=true v=true end for w,i in pairs(w:GetChildren())do S(i)end end end local function B()for w,i in pairs(f)do w.LocalTransparencyModifier=0 end f={}v=true C=nil if I then I:disconnect()I=nil end if c then c:disconnect()c=nil end for w,i in pairs(u)do i:disconnect()u[w]=nil end for w,i in pairs(T)do i:disconnect()T[w]=nil end end local function D(w)B()if I then I:disconnect()end I=w.DescendantAdded:connect(function(i)if V(i)then f[i]=true v=true elseif i:IsA("\084\111\111\108")then if u[i]then u[i]:disconnect()end u[i]=i.DescendantAdded:connect(function(w)f[w]=nil if w:IsA("\066\097\115\101\080\097\114\116")or w:IsA("\068\101\099\097\108")then w.LocalTransparencyModifier=0 end end)if T[i]then T[i]:disconnect()end T[i]=i.DescendantRemoving:connect(function(i)wait()if w and(i and i:IsDescendantOf(w))then if V(i)then f[i]=true v=true end end end)end end)if c then c:disconnect()end c=w.DescendantRemoving:connect(function(w)if f[w]then f[w]=nil w.LocalTransparencyModifier=0 end end)S(w)end function Y.SetEnabled(i,w)if k~=w then k=w i:Update()end end function Y.SetSubject(i,w)local Y=nil if w and w:IsA("\072\117\109\097\110\111\105\100")then Y=w.Parent end if w and(w:IsA("\086\101\104\105\099\108\101\083\101\097\116")and w.Occupant)then Y=w.Occupant.Parent end if Y then D(Y)else B()end end function Y.Update(Y)local I=false local c=tick()local u=workspace.CurrentCamera if u then local Y=0 if not k then I=true else local k=(u.Focus.p-u.CoordinateFrame.p).magnitude Y=(7-k)/5 if Y<.5 then Y=0 end if C then local W=Y-C if not I and(Y<1 and C<.95)then local Y=w*(c-o)W=i(-Y,Y,W)end Y=C+W else v=true end Y=i(0,1,W(Y,2))end if v or C~=Y then for w,i in pairs(f)do w.LocalTransparencyModifier=Y end v=false C=Y end end o=c end return Y end return o


local MAX_TWEEN_RATE = 2.8 -- per second

local function clamp(low, high, num)
	return (num > high and high or num < low and low or num)
end

local math_floor = math.floor

local function Round(num, places)
	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 = nil
		if subject and subject:IsA("Humanoid") then
			character = subject.Parent
		end
		if subject and subject:IsA("VehicleSeat") and subject.Occupant then
			character = subject.Occupant.Parent
		end
		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
