Need Help With Damage Code

Ok, so this works fine:
[lua]
function GM:ScalePlayerDamage(ply,hitgroup,dmginfo)
if dmginfo:GetAttacker():IsPlayer() then
dmginfo:GetAttacker():PrintMessage (3, "You hit “…ply:Name()…” for “…dmginfo:GetDamage()…” damage!
")
end
end
[/lua]

But, why doesn’t this work the same way ?
[lua]
function example(ply,hitgroup,dmginfo)
if dmginfo:GetAttacker():IsPlayer() then
dmginfo:GetAttacker():PrintMessage (3, "You hit “…ply:Name()…” for “…dmginfo:GetDamage()…” damage!
")
end
end

concommand.Add(“example”,example)
[/lua]
Then running “example” in the console?

Becuase ScalePlayerDamage is hooked to the engine. you can’t just create a function like that.

[editline]7th November 2010[/editline]

**[Gamemode.EntityTakeDamage

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.EntityTakeDamage)**

Ah alright, thank you. How could I make a console command that would do what the first code does? Actually, how would I write a console command that turns damage off/on?
I’ve gotten this far:
[lua]
function GM:ScalePlayerDamage(ply,hitgroup,dmginfo)
if dmginfo:GetAttacker():IsPlayer() then
dmginfo:SetDamage (0)
end
end
[/lua]
But, not even that works.

[ lua ] [ /lua ] without the spaces.

[lua]
function GM:EntityTakeDamage( ent, inf, att, amt )
if ( ent:IsPlayer() and att:IsPlayer() ) then
att:ChatPrint(“You hit " … ent:Nick() … " for " … amt … " damage”)
ent:ChatPrint(att:Nick() … " hit you for " … amt … " damage")

end

end
[/lua]

[editline]7th November 2010[/editline]

[lua]
Dmg = false

function GM:ScalePlayerDamage(ply,hitgroup,dmginfo)
if ply:IsPlayer() and Dmg then
dmginfo:SetDamage (0)
end
end

function toggledmg( pl )
if Dmg == false then
Dmg = true
else
Dmg = true
end
end
concommand.Add(“toggledmg”, toggledmg)
[/lua]

Ah alright, thanks a lot !

If your trying to retrieve data using a console command, such as attacker and damage I suggest using a table.

Your last edit was exactly what I was looking for! But, I put it in init.lua and tested it but it didn’t work.

[lua]
Dmg = false
function GM:EntityTakeDamage( ent, inf, att, amt, dmg )
if Dmg then dmg:ScaleDamage( 0 ) return false end
if ( ent:IsPlayer() and att:IsPlayer() ) then
att:ChatPrint(“You hit " … ent:Nick() … " for " … amt … " damage”)
ent:ChatPrint(att:Nick() … " hit you for " … amt … " damage")
end
end

function toggledmg( pl )
if Dmg == false then
Dmg = true
else
Dmg = true
end
end
concommand.Add(“toggledmg”, toggledmg)
[/lua]

Notice how it’s always true there… Might want to change it to this:
[lua]
function toggledmg( pl )
Dmg = !Dmg;
end
concommand.Add(“toggledmg”, toggledmg)
[/lua]

Thank you guys, I am now this far:
[lua]
Dmg = true
function GM:ScalePlayerDamage(ply,hitgroup,dmginfo)
if Dmg then
dmginfo:SetDamage (0)
Msg("Called Successfully
");
end
end

function toggledmg(ply)
Dmg = !Dmg;
if Dmg then
Msg("Damage is ON
");
else
Msg("Damage is OFF
");
end
end
concommand.Add(“toggledmg”, toggledmg)
[/lua]
And, I have placed this code in init.lua, but toggling it still doesn’t work, do i have it in the wrong place? Is there something wrong with the code?

EDIT: I have added the msgs to see if there was a problem calling the function or with the toggling, but everything works fine, so there must a problem with the dmginfo:SetDamage(0) line.

Dunno if this matters, but you might want to take out the space between SetDamage and (0). Also, try doing “return true” after you Msg("Called Successfully
");

[lua]
Dmg = true
function GM:ScalePlayerDamage(ply,hitgroup,dmginfo)
Msg( "Before: The Damage value was “…dmginfo:GetDamage()…”
" );
if Dmg then
dmginfo:SetDamage(0)
Msg("Called Successfully
");
Msg( "Now: The Damage value is “…dmginfo:GetDamage()…”
" );
end
end

function toggledmg(ply,dmginfo)
Dmg = !Dmg;
if Dmg then
Msg("Damage is ON(Dmg = True)
");
else
Msg("Damage is OFF(Dmg = False)
");
end
end
concommand.Add(“toggledmg”, toggledmg)
[/lua]
My friend hit me once with a crowbar and this was the output:
Before: The Damage value was 25
Called Successfully
Now: The Damage value is 0
Before: The Damage value was 0
Called Successfully
Now: The Damage value is 0

He hit me once again, after toggledmg was toggled, and this was the ouput:
] toggledmg
Damage is OFF(Dmg = False)
Before: The Damage value was 25
Before: The Damage value was 25

So, the toggle is working correctly, and all the functions are being called successfully, and it manages to change it to 0, how come it still shows 25 initially, and i still take the damage?

EDIT: The code is in init.lua