What hook can be called clientside that is run upon a player getting hurt?

Hello. I’ve been trying to find a hook that is called upon a player getting hurt, but will work clientside. Does one exist? If so, is it possible to create one?

function GM:PlayerHurt(ply)
if ply:IsBot() then
ply:ConCommand( "+forward" )

If you’re wondering why I’m trying to call a hook that is called if a player is hurt clientside, then here.(I know GM:PlayerHurt won’t work)


Or would this work?

if ply:IsBot() and ply:Health() < 100 then
ply:ConCommand( "+forward" )

Called shared when the player is hit


CLARIFICATION: What is a hook that can be called in an autorun file? Autorun files don’t seem to like GM:.

Note: the file that has this code is in: GarrysMod/garrysmod/lua/autorun.

If you are running it in an autorun file I’m assuming that you are making an addon meaning that you shouldn’t override hooks. Use

hook.Add instead.

I’m not making an addon. I put it in autorun just because I knew it works.

[editline]4th May 2015[/editline]

I get this error:

[ERROR] lua/autorun/dumb_fighting_bots.lua:2: attempt to index global ‘GM’ (a nil value)

  1. unknown - lua/autorun/dumb_fighting_bots.lua:2

Still, use the method I gave you, by making an addon I meant that you are making something that isn’t a gamemode. Because first of all you are initializing your file too soon and GM doesn’t exist yet and by doing function GM:HookName() end you are overriding the gamemode hook.

function PlayerHurtme(victim, attacker)
	if victim:IsBot() then
		victim:ConCommand( "+forward" )
hook.Add( "PlayerHurt", "test", PlayerHurtme )

Try this out

Okay, so baldursgate3’s code didn’t work, so I decided to use this:

 local function ControlBots(ply, cmd)
	--Ignore real players
	if ply:IsBot() then
	--Get the current keys pressed (probably none since they are a bot)
	local KeysPressed = cmd:GetButtons()
	--Add your keys into the mix
	KeysPressed = bit.bor( KeysPressed, IN_FORWARD)
	--Apply the keys to the movecommand

But, I get this error:

[ERROR] lua/autorun/dumb_fighting_bots.lua:6: attempt to call method ‘GetButtons’ (a nil value)

  1. v - lua/autorun/dumb_fighting_bots.lua:6
  2. unknown - lua/includes/modules/hook.lua:84

[editline]4th May 2015[/editline]

What I’m trying to get rid of is something that bots do, which is stand in place when they get hurt.

[editline]4th May 2015[/editline]

However, I’m trying to make the code only affect one bot, not every bot on the server.


function PlayerHurtme(victim, attacker)
	if victim:IsBot() then
		RunConsoleCommand( "+forward" )
hook.Add( "PlayerHurt", "test", PlayerHurtme )

Doesn’t work. The bot has movement codes used for it, so it still somehow uses them, meaning that it still counts as it is moving when it isn’t. How would I override this?

You cant use console commands on bots

What could I do to make them keep moving even if they have been shot?

Suprises nobody said this yet… Use GAMEMODE in place of GM

But the function already exists; he shouldn’t be trying to override it anyway.

If you want to move a bot when hit, you’ll need to code a NextBot or you can SetVelocity, if you just want it to move one time forward.

SetVelocity might be a good work-around for now. Of course I’ll probably end up using a different method in the future, but this will probably do for now.

Baulders, stop rating everyone dumb if you’re not going to provide a real solution.

Isn’t mass rating something that can get you banned?

Yeah, and just rating by itself can get you banned now. I rated some thread where someone died funny and I got a week.