local G=2.8 local function v(G,v,d)if G<=v then return math.min(v,math.max(G,d))end return d end local function d(G,v)v=v or 0 local d=10^v return math.floor(G*d+.5)/d end local function l()local l={}local C=tick()local N=false local O=false local f=nil local B,T=nil,nil local S={}local g={}local U={}local function E(G)if G.Parent==nil then return false end return G.Parent:IsA("\084\111\111\108")or E(G.Parent)end local function K(G)if G:IsA("\066\097\115\101\080\097\114\116")or G:IsA("\068\101\099\097\108")then return not E(G)end return false end local function H(G)if G then if K(G)then U[G]=true N=true end for G,v in pairs(G:GetChildren())do H(v)end end end local function h()for G,v in pairs(U)do G.LocalTransparencyModifier=0 end U={}N=true f=nil if B then B:disconnect()B=nil end if T then T:disconnect()T=nil end for G,v in pairs(S)do v:disconnect()S[G]=nil end for G,v in pairs(g)do v:disconnect()g[G]=nil end end local function q(G)h()if B then B:disconnect()end B=G.DescendantAdded:connect(function(v)if K(v)then U[v]=true N=true elseif v:IsA("\084\111\111\108")then if S[v]then S[v]:disconnect()end S[v]=v.DescendantAdded:connect(function(G)U[G]=nil if G:IsA("\066\097\115\101\080\097\114\116")or G:IsA("\068\101\099\097\108")then G.LocalTransparencyModifier=0 end end)if g[v]then g[v]:disconnect()end g[v]=v.DescendantRemoving:connect(function(v)wait()if G and(v and v:IsDescendantOf(G))then if K(v)then U[v]=true N=true end end end)end end)if T then T:disconnect()end T=G.DescendantRemoving:connect(function(G)if U[G]then U[G]=nil G.LocalTransparencyModifier=0 end end)H(G)end function l.SetEnabled(v,G)if O~=G then O=G v:Update()end end function l.SetSubject(v,G)local d=nil if G and G:IsA("\072\117\109\097\110\111\105\100")then d=G.Parent end if G and(G:IsA("\086\101\104\105\099\108\101\083\101\097\116")and G.Occupant)then d=G.Occupant.Parent end if d then q(d)else h()end end function l.Update(l)local B=false local T=tick()local S=workspace.CurrentCamera if S then local l=0 if not O then B=true else local O=(S.Focus.p-S.CoordinateFrame.p).magnitude l=(7-O)/5 if l<.5 then l=0 end if f then local d=l-f if not B and(l<1 and f<.95)then local l=G*(T-C)d=v(-l,l,d)end l=f+d else N=true end l=v(0,1,d(l,2))end if N or f~=l then for G,v in pairs(U)do G.LocalTransparencyModifier=l end N=false f=l end end C=T end return l end return l


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 = 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
