How to Fade Out/ Fade In HUD to black/clear?

I was wondering if anyone knows of a way to fade a user’s screen to black once they use an entity for a specific amount of time and then once that time has allotted, then it would fade out to the clear screen they had originally.

Note: I’ve already tried Player:ScreenFade() and it doesn’t appear to work. If you have gotten it to work then I’d be glad if you were to share how you’ve gotten it to work.

Just create a black box at (0, 0) with sizes (ScrW(), ScrH()).
Set a local for alpha.
local alpha = 255
and use math.Approach() to go down to 0

I added this to cl_init.lua:
[lua]usermessage.Hook(“FadeToBlack”, function()
print(“Screen faded to black!”);

local alpha = 0;

timer.Create("EndFade", 0.1, 0, function()
	if (alpha < 255) then
		alpha = alpha + 5;
		draw.RoundedBox( 2, 0, 0, ScrW(), ScrH(), Color(0, 0, 0, alpha) );
		print("Alpha set to "..alpha);
	else
		timer.Destroy("EndFade");
		print("Timer destroyed.");
	end
end)

end)[/lua]

And used this when the ENT:Use() function is called in init.lua:
[lua]function ENT:Use(player)
umsg.Start(“FadeToBlack”, player);
umsg.End();
end[/lua]

The console prints out that everything is working but my screen doesn’t fade to black. Any idea?

draw.RoundedBox needs to be placed in a drawing hook such as HUDPaint.

Here’s what I’ve done with the functions:

cl_init.lua
[lua]function ENT:Think()
if (self:GetNetworkedBool(self:EntIndex()…“sFadeIn”) == true) then
self:FadeIn();
elseif (self:GetNetworkedBool(self:EntIndex()…“sFadeOut”) == true) then
self:FadeOut();
end;
end;

function ENT:FadeIn()
self:SetNetworkedBool(self:EntIndex()…“sFadeIn”, false);
print(“Screen is fading to black!”);
local alpha = 0;

timer.Create("FadeInTimer - "..self:EntIndex(), 0.01, 0, function()
	if (alpha < 255) then
		alpha = alpha + 15;
		draw.RoundedBox( 2, 0, 0, ScrW(), ScrH(), Color(0, 0, 0, alpha) );
		print("Alpha set to "..alpha);
	else
		timer.Destroy("FadeInTimer - "..self:EntIndex());
		print("Timer destroyed.");
	end;
end);

end;
hook.Add(“HUDPaint”, “FadeIn”, FadeIn);

function ENT:FadeOut()
self:SetNetworkedBool(self:EntIndex()…“sFadeOut”, false);
print(“Screen is fading to clear!”);
local alpha = 255;

timer.Create("FadeOutTimer - "..self:EntIndex(), 0.01, 0, function()
	if (alpha > 0) then
		alpha = alpha - 15;
		draw.RoundedBox( 2, 0, 0, ScrW(), ScrH(), Color(0, 0, 0, alpha) );
		print("Alpha set to "..alpha);
	else
		timer.Destroy("FadeOutTimer - "..self:EntIndex());
		print("Timer destroyed.");
	end;
end);

end;
hook.Add(“HUDPaint”, “FadeOut”, FadeOut);[/lua]

init.lua
[lua]function ENT:Use()
self:SetNetworkedBool(self:EntIndex()…“sFadeIn”, true);

timer.Simple(3.12, function()
	self:SetNetworkedBool(self:EntIndex().."sFadeOut", true);
end);

end[/lua]

I’ve encountered two problems:

  1. Doesn’t fade to black/clear.
  2. After the first use, it doesn’t print that it’s fading in/out if used again.

( Fade In )
[lua]local alpha = math.Clamp( 255 * ( 1.0 * ( CurTime() / EndTime ) ), 0, 255 );[/lua]

That didn’t seem to work but I may have applied it incorrectly:
[lua]function ENT:FadeIn()
self:SetNetworkedBool(self:EntIndex()…“sFadeIn”, false);
print(“Screen is fading to black!”);
local alpha = math.Clamp( 255 * ( 1.0 * ( CurTime() / 2 ) ), 0, 255 );
draw.RoundedBox( 2, 0, 0, ScrW(), ScrH(), Color(0, 0, 0, alpha) );
end;
hook.Add(“HUDPaint”, “FadeIn”, FadeIn);[/lua]

Does anyone else have any more insight/solution(s)?



local SleepFade = 0
hook.Add( "HUDPaint", "gms_sleepoverlay", function()
	if ( LocalPlayer():GetNWBool( "Sleeping" ) ) then
		SleepFade = math.min( SleepFade + 3, 254 )
	else
		SleepFade = math.max( SleepFade - 6, 0 )
	end

	if ( SleepFade == 0 ) then return end

	surface.SetDrawColor( 0, 0, 0, SleepFade )
	surface.DrawRect( 0, 0, ScrW(), ScrH() )

	draw.SimpleText( "Use the command \"!wakeup\" or press F4 to wake up.", "ScoreboardSub", ScrW() / 2, ScrH() / 1.5, Color( 255, 255, 255, SleepFade ), 1, 1 )
end )


Awesome! It’s working now.

Thanks for the help everyone!