How to reset player's LocalTransparencyModifier value?

I wrote a module script that would show and hide the player’s body parts when a cutscene played and ended.

The script:

module.noHide=function()
	local plr = game.Players.LocalPlayer
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hum = char.Humanoid
	
	for childIndex,child in pairs(char:GetDescendants()) do
		if child:IsA("BasePart") then
			child:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
				child.LocalTransparencyModifier = child.Transparency
			end)
			child.LocalTransparencyModifier = child.Transparency
		end
	end
end

module.hidePls=function(val)
	local plr = game.Players.LocalPlayer
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hum = char.Humanoid
	for childIndex,child in pairs(char:GetDescendants()) do
		if child:IsA("BasePart") then
			child:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
				child.LocalTransparencyModifier = 1
			end)
			child.LocalTransparencyModifier = 1
		end
	end
end

The problem is the hidePls function doesn’t work and instead throws me this error:

Maximum event re-entrancy depth exceeded for Instance.LocalTransparencyModifierChanged

I have no idea how to reset the player’s local transparency modifier.

1 Like

I’m guessing whe nyou call both of them the issue occurs, in both of the functions you create an event connection that listens for when the LocalTransparencyModifier property is changed, and these connections change the LocalTransparencyModifer to different transparencies. They’ll both infinitely change the property until the script halts it. I would recommend having a table to store all the GetPropertyChangedSignal connections and disconnecting all of them before either of the loops so that there can only be 1 connection for each part at a time

Though if you never change the transparency modifier during cutscenes you can get rid of the connection code

I’d assume the problem is that you are changing the transparency modifier within the changed event, this would cause an infinite loop and you should add some sort of debounce.

After 3 grueling hours of watching YouTube tutorials and trial and error, I decided to use the renderstepped service and added a debounce to it.

local module = {}

module.noHide=function()
	local db = false
	local char = game:GetService("Players").LocalPlayer.Character
	game:GetService("RunService").RenderStepped:Connect(function()
		if not db then
			db = true
			for i, v in pairs(char:GetDescendants()) do
				if v:IsA("BasePart") then
					v.LocalTransparencyModifier = 0
					print(v.LocalTransparencyModifier)
				end
			end
		end
	end)
end

module.hidePls=function()
	local db = false
	local char = game:GetService("Players").LocalPlayer.Character
	game:GetService("RunService").RenderStepped:Connect(function()
		if not db then
			db = true
			for i, v in pairs(char:GetDescendants()) do
				if v:IsA("BasePart") then
					v.LocalTransparencyModifier = 1
					print(v.LocalTransparencyModifier)
				end
			end
		end
	end)
end
return module
1 Like