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)
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.
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.
Yeah, it’s pretty ugly 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
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)