Making the player slowly transparent when a button is clicked

this is a script to add sound, particles and damage to the player when the part is clicked. they all work FINE so no need to change those scripts, only to be added is transparency to the player, when you click the button you slowly become more and more transparent untill you are finally invisible.

local deb = false
local rate = 7 -- measured in health / s; player loses 7 health per second

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
     if not deb then
     deb = true
     script.Parent.woosh.SoundId = "rbxassetid://1839756886"
     script.Parent.woosh:Play()
     for i, v in pairs(plr.Character:GetDescendants()) do
         if  v:IsA("BasePart") then
            local particle = script.Parent.ParticleEmitter:Clone()
            particle.Enabled = true
            particle.Parent = v
         end
     end
     spawn(function()
     while (plr.Character.Humanoid.Health > 0) do
         local wt = wait()
         plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - (wt * rate)
     end
     end)
     end
     wait(100 / rate) -- if you ever decide to change the rate
     deb = false
end)

I don’t think you have to make separate topics. Replace the while loop in the code with this:

 while (plr.Character.Humanoid.Health > 0) do
      local wt = wait()
      for _,v in pairs(plr.Character:GetDescendants()) do
          if v:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then
              v.Transparency = 1 - (plr.Character.Humanoid.Health / plr.Character.Humanoid.MaxHealth)
          end
      end
      plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - (wt * rate)
end

for _,v in pairs(plr.Character:GetDescendants()) do
    if v:IsA("BasePart") then
         v.Transparency = 1
    end
end

Also, change this wait(100 / rate) to wait(plr.Character.Humanoid.MaxHealth / rate), if the players’ MaxHealth is not 100

Here’s a simple addition that should work fine:

local deb = false
local rate = 7 -- measured in health / s; player loses 7 health per second

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
     if not deb then
     deb = true
     script.Parent.woosh.SoundId = "rbxassetid://1839756886"
     script.Parent.woosh:Play()
     for i, v in pairs(plr.Character:GetDescendants()) do
         if  v:IsA("BasePart") then
            local particle = script.Parent.ParticleEmitter:Clone()
            particle.Enabled = true
            particle.Parent = v
         end
     end
     local tran = 0 --Transparency
     spawn(function()
     while (plr.Character.Humanoid.Health > 0) do
         local wt = wait()
         plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - (wt * rate)
         tran = tran + (wt * rate * 0.01)
        for _, part in pairs(plr.Character:GetDescendants()) do
            if part:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then --Added check for humanoid root part
                part.Transparency = tran
            end
        end
     end
     end)
     end
     wait(100 / rate) -- if you ever decide to change the rate
     deb = false
end)

This method makes the transparency change at the same relative rate as the health, (01).

Edit: Added check for HumanoidRootPart


@Raretendoblox
Your solution will work if you move the

for _,v in pairs(plr.Character:GetDescendants()) do
     if v:IsA("BasePart") then
          v.Transparency = 1
     end
end

outside of the while loop.

Otherwise, the character will become instantly invisible.

1 Like

I just realised now – because i’ve been constantly editing the script, I put it in the loop. Thanks for pointing that out

1 Like

this works fine but when becoming transparent the hitbox begins to show, is there any way to counter it?

What do you mean by “hitbox”?


Ah, the HumanoidRootPart.

I assume you mean the HumanoidRootPart; change this portion of his script:

for _, part in pairs(plr.Character:GetDescendants()) do
            if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then
                part.Transparency = tran
            end
        end
1 Like

did that, now i dont become transparent at all

Made a little mistake in the script, so if you run that, it should error. I’ve made edits to my reply above – check it out

clumsy me.

1 Like

should i post the full script and put that as solution? since there were different pieces to put together

You can but it’s up to you, as long the code works as you intended it to be with no faults and errors, since everything is kind of “all over the place”. This may help others with similar problems

alright, if someone in the future needs it heres the full script:

‘’
local deb = false
local rate = 7 – measured in health / s; player loses 7 health per second

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
if not deb then
deb = true
script.Parent.woosh.SoundId = “rbxassetid://1839756886”
script.Parent.woosh:Play()
for i, v in pairs(plr.Character:GetDescendants()) do
if v:IsA(“BasePart”) then
local particle = script.Parent.ParticleEmitter:Clone()
particle.Enabled = true
particle.Parent = v
end
end
local tran = 0 --Transparency
spawn(function()
while (plr.Character.Humanoid.Health > 0) do
local wt = wait()
plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - (wt * rate)
tran = tran + (wt * rate * 0.01)
for _, part in pairs(plr.Character:GetDescendants()) do
if part:IsA(“BasePart”) and part.Name ~= “HumanoidRootPart” then
part.Transparency = tran
end
end
end
end)
end
wait(100 / rate) – if you ever decide to change the rate
deb = false
end)
‘’