When Damaged, GUI Doesn't Fade Away After Appearing

I’m trying to make an image appear when you get hurt. I have a script below of what I am trying to do. I want it to appear and then fade out and disappear but it only appears and doesn’t disappear . I’m not sure what I’m doing wrong. (keep in mind i’m pretty bad at scripting)

local plr = game.Players.LocalPlayer
local char = plr.Character
local humanoid = char.Humanoid
local oldHealth = humanoid.Health
local sound = char:WaitForChild("DamageSound")

humanoid.HealthChanged:Connect(function(health)
	if oldHealth > health then
		plr.PlayerGui.HurtScreen.Enabled = true
		local newSound = sound:Clone()
		newSound.PlayOnRemove = true
		newSound.Parent = char
		newSound:Destroy()
	end
	oldHealth = health
	while plr.PlayerGui.HurtScreen.vignette.BackgroundTransparency > 0  do
		plr.PlayerGui.HurtScreen.vignette.BackgroundTransparency = plr.PlayerGui.HurtScreen.vignette.BackgroundTransparency - 0.1
	end
end)

I have a picture of where the script is and where the screen gui is. (Don’t mind the part where it plays a sound, that works perfectly fine in the script)
asdasdasd

Any help can help!

Decreasing transparency makes a GUI element less transparent while increasing it makes it more transparent so if you want something to fade out you should increase the transparency.

	while plr.PlayerGui.HurtScreen.vignette.BackgroundTransparency < 1  do
		plr.PlayerGui.HurtScreen.vignette.BackgroundTransparency = plr.PlayerGui.HurtScreen.vignette.BackgroundTransparency + 0.1
		wait(0.01)
	end

edit: also make sure to add a wait() or else the fade will be instant.

1 Like

Ohhh ok, I’ll try this when I get home ty for responding so fast!

I’m not sure if this will fix your issue but I would try this

local plr = game.Players.LocalPlayer
local char = plr.Character
local humanoid = char.Humanoid
local oldHealth = humanoid.Health
local sound = char:WaitForChild("DamageSound")

humanoid.HealthChanged:Connect(function(health)
	if oldHealth > health then
		plr.PlayerGui.HurtScreen.Enabled = true
		local newSound = sound:Clone()
		newSound.PlayOnRemove = true
		newSound.Parent = char
		newSound:Destroy()

                oldHealth = health

                task.wait(.5) -- Amount of time before it fades away
 game:GetService("TweenService"):Create(plr.PlayerGui.HurtScreen.vignette, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {BackgroundTransparency = 1}):Play()
	      
	end
end)

Not sure if this is formatted right or whatever I’m on phone so excuse me for that

1 Like

Also yeah what the other guy said, 1 transparency = you can’t see it and 0 transparency = you can see it

1 Like

Thanks, I’ll also try this when I get home since I’m on my way to see fireworks lol

Ohh duh. Sometimes I make the dumbest mistakes and then I feel really stupid. I knew that 1 transparency means you can’t see it but I just rush through things that I don’t pay attention! Thank you!

1 Like

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