Reverse LocalTransparencyModifier

Hey everyone I made this code that makes the character’s body visible while in first person:

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()

--// Show Player's Body
script.Parent.ShowBody.Changed:Connect(function()
	if script.Parent.ShowBody.Value == true then
		game:GetService("RunService").RenderStepped:connect(function()
			if Char:FindFirstChild("RightUpperArm") and Char:FindFirstChild("LeftUpperArm") then
				Char:FindFirstChild("RightUpperArm").LocalTransparencyModifier = 0
				Char:FindFirstChild("RightLowerArm").LocalTransparencyModifier = 0
				Char:FindFirstChild("RightHand").LocalTransparencyModifier = 0
				Char:FindFirstChild("LeftUpperArm").LocalTransparencyModifier = 0
				Char:FindFirstChild("LeftLowerArm").LocalTransparencyModifier = 0
				Char:FindFirstChild("LeftHand").LocalTransparencyModifier = 0
				Char:FindFirstChild("RightUpperLeg").LocalTransparencyModifier = 0
				Char:FindFirstChild("RightLowerLeg").LocalTransparencyModifier = 0
				Char:FindFirstChild("RightFoot").LocalTransparencyModifier = 0
				Char:FindFirstChild("LeftUpperLeg").LocalTransparencyModifier = 0
				Char:FindFirstChild("LeftLowerLeg").LocalTransparencyModifier = 0
				Char:FindFirstChild("LeftFoot").LocalTransparencyModifier = 0
				Char:FindFirstChild("UpperTorso").LocalTransparencyModifier = 0	
				Char:FindFirstChild("LowerTorso").LocalTransparencyModifier = 0	
				Char:FindFirstChild("Humanoid").CameraOffset = Vector3.new(0,0,-1)
			end
		end)
	else
		--do stuff
	end
end)

However, how do I revert it back to the normal, in case the ShowBody.Value == false
Just changing the LocalTransparencyModifier to 1 won’t work… Thank you for your time!

Your script is checking if ShowBody is on, then connecting to RenderStepped; but the RenderStepped has no conditions that check to see if ShowBody is still true. Instead, every time ShowBody’s value is set to true, a new RenderStepped connection is made that just hides the player again, with nothing to disconnect it. Here’s what I would do instead:

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local ShowBodyValue = script.Parent.ShowBody

--// Show Player's Body
game:GetService("RunService").RenderStepped:Connect(function()
	--// [variable name] = [condition] and [result when true] or [result when false]
	local ShowingBody = (ShowBodyValue.Value == true)
	local TransparencyModifier =  ShowingBody and 0 or 1
	local HumanoidCamOffset = ShowingBody and Vector3.new(0, 0, -1) or Vector3.zero
	if Char:FindFirstChild("RightUpperArm") and Char:FindFirstChild("LeftUpperArm") then
		Char:FindFirstChild("RightUpperArm").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("RightLowerArm").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("RightHand").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("LeftUpperArm").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("LeftLowerArm").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("LeftHand").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("RightUpperLeg").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("RightLowerLeg").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("RightFoot").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("LeftUpperLeg").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("LeftLowerLeg").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("LeftFoot").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("UpperTorso").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("LowerTorso").LocalTransparencyModifier = TransparencyModifier
		Char:FindFirstChild("Humanoid").CameraOffset = HumanoidCamOffset
	end
end)

You’d also benefit from using a for loop to set all those LocalTransparencyModifiers, so you should look into those too, but this is the general idea.

1 Like

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