Tool Transparency bug

I am making a tool that makes your body transparent when you drink it. But the thing is, it glitches whenever I try. The head and torso changes transparency, but it glitches after it goes to the left arm.

The Code:

local tool = script.Parent
local player = tool.Parent.Parent

local enabled = true
local drinkTrack

function onActivated()
	if not enabled then
		return
	end

	enabled = false

	tool.Handle.DrinkSound:Play()
		--play drink animation
	
		local head = player.Character:FindFirstChild("Head")
		local torso = player.Character:FindFirstChild("Torso")
		local leftleg = player.Character:FindFirstChild("LeftLeg")
		local rightleg = player.Character:FindFirstChild("RightLeg")
		local leftarm = player.Character:FindFirstChild("LeftArm")
		local rightarm = player.Character:FindFirstChild("RightArm")
		head.Transparency = 0.8
		torso.Transparency = 0.8
		leftleg.Transparency = 0.8
		rightleg.Transparency = 0.8
		leftarm.Transparency = 0.8
		rightarm.Transparency = 0.8
		

task.wait(5)
		enabled = true
		head.Transparency = 0
		torso.Transparency = 0
		leftleg.Transparency = 0
		rightleg.Transparency = 0
		leftarm.Transparency = 0
		rightarm.Transparency = 0
	end

tool.Activated:connect(onActivated)

The bug:

Hopefully people reply, I will read everyones replies after I get back online. I’m going offline now.

Neither of the rigs have ‘RightArm’ or any of the ribs you spelt there.

For example, in R6 there is ‘Right Arm’. [With space].

2 Likes

Oh god, I know you think that is ugly too. Try using a loop instead:

local Char = player.Character or Player.CharacterAdded:Wait()
for _, Part in Pairs(Char:GetDescendants()) do
    if not Char:IsA("BasePart") then continue end
    Part.Transparency = 0.8
end

That should work. Also, like @Valkyrop mentioned, you missed the spaces between the words, however, not only does the loop approach look cleaner, it also prevents against mistakes like that.

3 Likes

Yes, as @domboss37 said, you don’t need to list every single body part. If you put a for loop like the one above, It will check to see if all children in the character are base parts. If it isn’t, it will skip the function and put the rest of the base parts’ transparency to true.

1 Like

Yeah, it’s pretty ugly :joy: but, that script never ended up working. No transparency changes and you made some spelling mistakes, Player.CharacterAdded should be player.CharacterAdded and in Pairs should be in pairs. here’s the script, I finished your spelling mistakes

	local Char = player.Character or player.CharacterAdded:Wait()
	for _, Part in pairs(Char:GetDescendants()) do
		if not Char:IsA("BasePart") then continue end
		Part.Transparency = 0.8
	end

Ok, we understand that @domboss37 may have made some spelling mistakes but you should mark his answer as solution

Thanks, it worked but I want to find a more clean way to do it.

But it didn’t work.

Summary

This text will be hidden

Try this one.

local character = player.Character

for _, v in pairs(character:GetChildren()) do
	if v:IsA("Part") then
		v.Transparency = 0.8
	end
end
1 Like

This will work but it will also change the player’s humanoid root part transparency.

If you don’t want that to happen, you could change if condition to:

if v:IsA("Part") and v.Name ~= "HumanoidRootPart" then
	v.Transparency = 0.8
end

The solution you are looking for is then (slightly more elegant than @Infernust’s script:

local Char = player.Character or player.CharacterAdded:Wait()
for _, Part in pairs(Char:GetDescendants()) do
    if not Char:IsA("BasePart") and Part ~= Char.PrimaryPart then continue end
    Part.Transparency = 0.8
end

Note: this will also account for Accessories, if you don’t want that, change the script to Use: GetChildren().

Also:

Yeah sorry about that, I usually declare constants (non-changing variables with a uppercase, hence the capital P in player) as for pairs, idk why I did that (I wrote that on mobile btw)