Apply alpha to drawn box

Hello dear Facepunch-Forum,

I’m not programming anything in LUA too much, but now I wanted to try something. I wanted to make it so that if the player dies the screen fades to black. Simple, right? Yea, but the problem is that the alpha (that changes with the help of a timer) doesn’t want to apply to the box I draw over the whole screen. So can you help me please? That’s my code so far:


local playerdead = false;
local alpha = 0;
    timer.Create("timer1",0.1,0,function()
        if playerdead == true then
//RunConsoleCommand("say", "Alpha = "..alpha) --Used this to test if the alpha even changes, and yes: It does.
	        if alpha > -1 then
	    	alpha = alpha+2
	        end
        end
		
       if alpha > 255 then
	       timer.Destroy("timer1")
       end
    end);

function fade1()
	draw.RoundedBoxEx(1,0,0,ScrW(),ScrH(),Color(0,0,0,alpha)) --That's the line that causes trouble
end
	hook.Add("PlayerDeath","Check if player is dead", function() playerdead = true end)
	hook.Add("HUDPaint","Fade",fade1)

If I change the alpha to a number instead of alpha it works, but that’s not the point because it doesn’t change then. I hope you can help me.

Thank you,
DigitalClark.

i let my self re-write it completely, hope thats ok with you :slight_smile:



local alpha = 0

function HUDPaint()
	
	if not LocalPlayer():Alive() then
		
		draw.RoundedBoxEx(1,0,0,ScrW(),ScrH(),Color(0,0,0,alpha))
		if alpha < 255 then alpha = alpha + 1 end
		
	else
	
		alpha = 0 -- if player is alive reset alpha
		
	end
	
end
hook.Add("HUDPaint", "HUDPaint", HUDPaint)


I did fail to debug what was your problem but generally you need more practice with lua :stuck_out_tongue: for example you dont need semicolons in lua.

Alpha means that it’s 0-255.
255 being entirely opaque,
0 meaning it’s 100% translucent.

Thanks! Now it works!

I know that, but thanks anyway :D!

read the post before you answer wtf…