Tweening Overlapping

I have a localscript in starterplayerscripts, basically, because my camera is angled top-down, the player can sometimes be blocked by buildings/trees or anything. So if the player is blocked by a wall examole, that wall will be faded to 0.7 transparency using tweenservice smoothly so the player is visible again.

Problem is that if the player repeatedly gets blocked/unblocked from vision, the object that’s blocking will get permanently slightly transparent. Example: the player is at the door of a building, and the player enters and exits the building at a very fast rate because he is spamming A and D, making the building permanently a bit transparent.

I’ve used chatgpt to fix it but seems like chatgpt is making the code worse so uhh… here’s the script after being modified with gpt

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

-- Fade settings
local FADE_TRANSPARENCY = 0.7
local FADE_TIME = 0.25
local EPSILON = 0.01 -- Used to prevent redundant tweening

-- Tracking
local fadedParts = {}
local activeTweens = {}

-- Clamp to avoid drifting
local function clampTransparency(val)
	return math.clamp(math.round(val * 1000) / 1000, 0, 1)
end

-- Tween helper with safety
local function tweenTransparency(part, to)
	if not part:IsA("BasePart") then return end
	local current = part.Transparency

	-- Don't tween if already close to target
	if math.abs(current - to) < EPSILON then return end

	-- Cancel previous tween
	if activeTweens[part] then
		activeTweens[part]:Cancel()
	end

	local tween = TweenService:Create(part, TweenInfo.new(FADE_TIME), {Transparency = to})
	activeTweens[part] = tween
	tween:Play()

	tween.Completed:Connect(function()
		if activeTweens[part] == tween then
			activeTweens[part] = nil
		end
		-- Clamp transparency just in case
		part.Transparency = clampTransparency(part.Transparency)
	end)
end

-- Fade all parts in a model (except NoFade ones)
local function fadeModel(model)
	for _, part in ipairs(model:GetDescendants()) do
		if part:IsA("BasePart") and not CollectionService:HasTag(part, "NoFade") then
			if fadedParts[part] == nil then
				fadedParts[part] = part.Transparency
			end
			tweenTransparency(part, FADE_TRANSPARENCY)
		end
	end
end

-- Restore all faded parts
local function unfadeAll()
	for part, original in fadedParts do
		if part and part.Parent then
			tweenTransparency(part, original)
		end
	end
	fadedParts = {}
end

-- Get part blocking view (ignore NoFade)
local function getBlockingHit()
	local char = player.Character
	if not char or not char:FindFirstChild("HumanoidRootPart") then return nil end

	local origin = camera.CFrame.Position
	local target = char.HumanoidRootPart.Position

	local ignoreList = {char}
	for _, part in ipairs(workspace:GetDescendants()) do
		if CollectionService:HasTag(part, "NoFade") then
			table.insert(ignoreList, part)
		end
	end

	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = ignoreList

	local result = workspace:Raycast(origin, (target - origin), params)
	return result
end

-- Main loop
RunService.RenderStepped:Connect(function()
	local hitResult = getBlockingHit()
	if hitResult then
		local model = hitResult.Instance:FindFirstAncestorOfClass("Model")
		if model and model ~= player.Character then
			fadeModel(model)
		end
	else
		unfadeAll()
	end
end)

Original code (before modified):

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

-- Fade settings
local FADE_TRANSPARENCY = 0.7
local FADE_TIME = 0.25

-- Track faded parts and original transparencies
local fadedParts = {}
local activeTweens = {}

-- Safely tween part transparency, canceling previous tweens
local function tweenTransparency(part, to)
	if not part:IsA("BasePart") then return end

	-- Cancel existing tween
	if activeTweens[part] then
		activeTweens[part]:Cancel()
	end

	local tween = TweenService:Create(part, TweenInfo.new(FADE_TIME), {Transparency = to})
	activeTweens[part] = tween
	tween:Play()

	-- Clean up when done
	tween.Completed:Connect(function()
		if activeTweens[part] == tween then
			activeTweens[part] = nil
		end
	end)
end

-- Fade all parts in a model (except NoFade ones)
local function fadeModel(model)
	for _, part in ipairs(model:GetDescendants()) do
		if part:IsA("BasePart") and not CollectionService:HasTag(part, "NoFade") then
			if fadedParts[part] == nil then
				fadedParts[part] = part.Transparency
				tweenTransparency(part, FADE_TRANSPARENCY)
			end
		end
	end
end

-- Restore all faded parts to original transparency
local function unfadeAll()
	for part, original in fadedParts do
		if part and part.Parent then
			tweenTransparency(part, original)
		end
	end
	fadedParts = {}
end

-- Get part blocking the view (ignores NoFade parts)
local function getBlockingHit()
	local char = player.Character
	if not char or not char:FindFirstChild("HumanoidRootPart") then return nil end

	local origin = camera.CFrame.Position
	local target = char.HumanoidRootPart.Position

	local ignoreList = {char}
	for _, part in ipairs(workspace:GetDescendants()) do
		if CollectionService:HasTag(part, "NoFade") then
			table.insert(ignoreList, part)
		end
	end

	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	rayParams.FilterDescendantsInstances = ignoreList

	local result = workspace:Raycast(origin, (target - origin), rayParams)
	return result
end

-- Main loop
RunService.RenderStepped:Connect(function()
	local hitResult = getBlockingHit()
	if hitResult then
		local model = hitResult.Instance:FindFirstAncestorOfClass("Model")
		if model and model ~= player.Character then
			fadeModel(model)
		end
	else
		unfadeAll()
	end
end)

Using the original code, I added:
if fadedParts[part] == nil and activeTweens[part] == nil then
to the ‘fadeModel’ function
which seemed to resolve the problem
hope this helps!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.