Code problem


local function HolsterScript( target, dmginfo )
    if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then
	    target:PrintMessage( HUD_PRINTCONSOLE, "say /holster" )
    end
end

hook.Add("PlayerTraceAttack", "holsterscript", HolsterScript)

Anyone know why this isnt working? The goal is to make me say /holster when i take bullet damage. Thanks in advance. I couldve used EntityTakeDamage, but it was serverside and im trying to make a clientside script.

Firstly, you should be using EntityTakeDamage serverside – the message would be networked anyway. Secondly, PrintMessage doesn’t actually register the PlayerSay hook to be called, but you shouldn’t be forcing the player to chat either way: just call the function “/holster” is attached to.

Im trying to use a clientside script on a server, you cant run serverside ones with lua_openscript_cl so PlayerTraceAttack is the closet thing i can find to EntitiyTakeDamage. I have a working one for serverside, but i need a clienside one. Also, /holster is just a command I wanted to be said in chat for the script so I need a way to say it in chat.

[editline]17th August 2017[/editline]

I have this:


local function HolsterScript( target, dmginfo )
    if ( target:IsPlayer() and dmginfo:IsBulletDamage() ) then
	    target:PrintMessage( HUD_PRINTTALK, "/holster." )
    end
end

hook.Add("EntityTakeDamage", "holsterscript", HolsterScript)

Everything works except for the printtalk (shows up in chat but not in the Y chat). But it only works in autorun/server. I need it to work in the lua folder.

[lua]local hp = LocalPlayer():Health()
hook.Add( “Think”, “holsterscript”, function()
if not LocalPlayer():Alive() then return end

local newhp = LocalPlayer():Health()
if hp ~= newhp then
    RunConsoleCommand( "say", "/holster" )
    hp = newhp
end

end )[/lua]

or timer based

[lua]local hp = LocalPlayer():Health()
timer.Create( “holsterscript”, 1, 0, function()
if not LocalPlayer():Alive() then return end

local newhp = LocalPlayer():Health()
if hp ~= newhp then
    RunConsoleCommand( "say", "/holster" )
    hp = newhp
end

end )[/lua]

[editline]18th August 2017[/editline]

Also this is a really shit request. it really should be handled on the serverside.

[editline]18th August 2017[/editline]

Plus it doesn’t account for if you gained HP. so you’d need to check hp > newhp before running the command

Shit request or not, I appreciate the help

[lua]if hp > newhp then RunConsoleCommand( “say”, “/holster” ) end[/lua]
Means previous hp was greater than new hp, or that the player just took damage from something.

You can not check if it was a bullet because clientside doesn’t have access to this information.
If you have “server” errors, put in if CLIENT then <all the code> end

All i needed was that doesnt matter if it was bullet or anything. Ill have to try this in a bit as im busy right now but thanks.

[editline]18th August 2017[/editline]

Works perfectly