Death lua scripting


I have some idea about creating a LUA script but I’m a beginner in this domain and I have no idea how to start.

To summarize, I want a script running when the player die, playing sound and displaying a post processing effect. If you can give me an example or even a tutorial of this kind of script it would be helpful.

Thanks for advance.

Hooks are used to run functions when certain events happen in the game. In your case, you can use the PlayerDeath hook to do what you want whenever a player dies.
[lua]hook.Add( “PlayerDeath”, “RandomNameYouGiveToYourHook”, function( victim, weapon, killer )
// code
end )[/lua]

This hook returns three arguments, seen in the function, the person receiving the damage (i.e. the victim), the entity used to deal the damage (i.e. the weapon) and the owner of said weapon ( i.e. the killer ).

EmitSound is the function to emit a sound from the given the player’s current location.
[lua] LocalPlayer():EmitSound( “sound.wav” ) [/lua]
This will play the sound locally, so only the player from which this code was run will hear it.
If you’re running this serverside, which you will need to do for the hook to be able to be used, you’ll have to supply the player, and LocalPlayer() can’t be used. If the code is run server-side, you can also make the sound be heard by other players withing a certain range of the player the sound was emitted from, check the link for more info.

The issue with that, is that PlayerDeath is a SERVER-side hook.

Here’s what I do to draw death effects on the client

function GM:RenderDeathEffects( )
if ( !LocalPlayer( ):Alive( ) ) then
DrawSobel( 0.0001 )
DrawMotionBlur( 0.1, 0.79, 0.05)
local tab = {}
tab[ “$pp_colour_addr” ] = 0
tab[ “$pp_colour_addg” ] = 0
tab[ “$pp_colour_addb” ] = 0
tab[ “$pp_colour_brightness” ] = 0
tab[ “$pp_colour_colour” ] = 0
tab[ “$pp_colour_contrast” ] = 1
tab[ “$pp_colour_mulr” ] = 0
tab[ “$pp_colour_mulg” ] = 0
tab[ “$pp_colour_mulb” ] = 0

	DrawColorModify( tab )
	-- local tab = {}
	-- tab[ "$darken" ] = 1
	-- tab[ "$multiply" ] = 0
	-- DrawDownSample( tab )

hook.Add( “RenderScreenspaceEffects”, “RenderDeathEffects”, GM.RenderDeathEffects )[/lua]

As for optimizing this so it’s not always detecting if the player is !Alive, you could use PlayerDeath, network some code to the client to start the hook, start playing sound, etc. And then it can monitor until the player is alive, when the player is alive, remove the hook and the sound!

Net messaging tutorial

I want to get an flash effect with the bloom (start the darken amount from 1.00 and finish to 0.00) and the screen fade to black.

I’m also trying to put "game.ConsoleCommand("host_timescale 0.35
“)” but it says “attempt to call field ‘ConsoleCommand’ (a nil value)”

[lua]ply:ConCommand( “command”, “arg1” )[/lua] << Clientside
[lua]RunConsoleCommand( “command”, “arg1” )[/lua] << Serverside

Another problem: my function is looping so it allays restart my timer (and stuck in this function) until I’m revived.

Post the code, I can suggest a fix.

I also having a problem to keep displaying the text -_-’

[lua]hook.Add(“PlayerDeathSound”, “DeFlatline”, function() return true end)

local noise = Sound(“wasted.wav”)

function RenderDeathEffects()
if ( !LocalPlayer( ):Alive( ) ) then

	local tab = {} 
	local WastedText = surface.GetTextureID( "vgui/wasted" );

	tab[ "$pp_colour_addr" ] = 0
	tab[ "$pp_colour_addg" ] = 0
	tab[ "$pp_colour_addb" ] = 0
	tab[ "$pp_colour_brightness" ] = 0
	tab[ "$pp_colour_colour" ] = 0.30
	tab[ "$pp_colour_contrast" ] = 1
	tab[ "$pp_colour_mulr" ] = 0
	tab[ "$pp_colour_mulg" ] = 0
	tab[ "$pp_colour_mulb" ] = 0 
	DrawColorModify( tab ) 
	RunConsoleCommand("host_timescale", "0.35"); 
	-- local tab = {}
	-- tab[ "$darken" ] = 1
	-- tab[ "$multiply" ] = 0
	-- DrawDownSample( tab )
	timer.Create("step2", 0.9, 1, function ()
		RunConsoleCommand("host_timescale", "0.1"); 
		timer.Create("end", 0.5, 1, function()
			RunConsoleCommand("host_timescale", "1")

hook.Add(“PlayerDeath”, “NewSound”, function(vic,unused1,unused2) vic:EmitSound(noise) end)
hook.Add( “RenderScreenspaceEffects”, “RenderDeathEffects”, RenderDeathEffects )
hook.Add(“HUDPaint”,“DrawTheText”,drawtext) [/lua]