Tweening a player character's Transparency on taking damage

Greetings all! I am currently working on a script that causes the player (or more specifically, the player’s character) to temporarily fade to translucent and back when they take damage. Although the script itself produces no errors, I have been unfortunate in getting it to work after having attempted to test it out with the Humanoid:TakeDamage function in Play mode in Studio. As of right now I have not found any solutions to this after having searched for some time on and off the DevForum and I could do with some assistance in searching for the issue. Any and all suggestions and advice would be greatly appreciated!

ExplorerHighlight

The above image is the current location in the Explorer in which the script is located. (highlighted in yellow for emphasis)

--// Variables
local Players = game:GetService("Players") -- Get Player service
local TweenService = game:GetService("TweenService") -- Get Tween service
debounce = false

--// Function
Players.PlayerAdded:Connect(function(player) -- Calls function when a player joins
	player.CharacterAdded:Connect(function(character) -- Calls nested function when player character spawns
		local humanoid = character:WaitForChild("Humanoid")
		local humanoidHealth = humanoid.Health
		
		humanoid.HealthChanged:Connect(function(Health) -- Calls nested function when the player's health changes
			if debounce then return end
			if humanoidHealth < Health then -- Fires function if player's current health is lower than the stored value 
				debounce = true
				for i, v in pairs(player.Character:GetDescendants()) do
					if v:IsA("BasePart") or v:IsA("MeshPart") then
						
						--// Tween Info & Goal
						local tweenInfo = TweenInfo.new(
							0.35, -- Time
							Enum.EasingStyle.Linear, -- EasingStyle
							Enum.EasingDirection.InOut, -- EasingDirection
							0, -- RepeatCount
							true, -- Reverses
							0 -- DelayTime
						)

						local tweenGoals = {Transparency = 0.6}
						
						local tween = TweenService:Create(v, tweenInfo, tweenGoals)
						tween:Play()
					end
				end
			debounce = false -- Resets debounce
			humanoidHealth = Health	-- Updates variable for the next function call
			end
		end)
	end)
end)

it’s because of this line

if humanoidHealth < Health then

humanoidHealth at first would be the max health and Health would be the current health, this line would only be true if the character’s health goes pass their max health, just replace it with

if Health < humanoidHealth then
1 Like

Thanks for the heads-up! I’ve gone ahead and made that change to the script. On the other hand, the solution itself is yet distant—as of now when I tested the script again using Humanoid:TakeDamage, the tween still does not play. Could there be another issue within the script that I may have overlooked or is there an issue elsewhere?

are you running the command while on the client view? i.e this this is visible, if so just click the aforementioned icon to change the view to server view and run the command there
the script is a server script so it wouldn’t detect changes done on the client

1 Like

Whoops! It appears that I was, in fact, running the command on client view. The tween works as intended on server view! Thanks once again for the help there. Although, if I wanted to also replicate this on the client side as well, would I just have to replicate the same script but as a local script? And what would be a good solution to make sure the HumanoidRootPart does not also become visible?

Anything done on the server is automatically replicated to the client.

1 Like

if you want the effect to happen when the damage is done on the client you can place a LocalScript somewhere, preferably in StarterGui and detects when the local character’s humanoid health is changed

local localPlayer = game.Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait() --the 'or' happens if localPlayer.Character is nil (meaning the character hasn't been loaded yet), which would stops the script until the character is added
local humanoid = character:WaitForChild("Humanoid")
humanoid.HealthChanged:Connect(function(Health)
    --rest of script goes here
end)

as for making the humanoid root part not visible, just do

if (v:IsA("BasePart") or v:IsA("MeshPart")) and v.Name ~= "HumanoidRootPart" then
1 Like