I just have a quick question. I want to make a function. What it does is when a player gets damaged, it checks if the attacker is in team 1 and if so, it sets the attackers player color to red. I don't know what function/hook I need to use.
This has not been tested, but I just ripped some of my admin mod code and changed some of it.
[CODE]function PlayerDamage(target,damageinfo)
if (target:GetClass():lower() == "player") and (damageinfo:GetAttacker():GetClass():lower() == "player") and (damageinfo:GetAttacker() != target) then
if(damageinfo:GetAttacker():Team() != target:Team()) then damageinfo:GetAttacker():SetColor(Color(255,0,0)) end
end
end
hook.Add("EntityTakeDamage","MunDamage",PlayerDamage)[/CODE]
monkstick's code should work, but I just had to re-write it. Particularly because it was making PlayerDamage a global function.
[lua]local function PlayerDamage(target,damageinfo) --Don't make your function global
local atkr = damageinfo:GetAttacker() --just makes for shorter code
if IsValid(target) and target:IsPlayer() and IsValid(atkr) and atkr~=target and atkr:IsPlayer() then --target is a player, atkr is valid, atkr is not the target, atkr is a player
if atkr:Team() == 1 then --atkr is in team 1
atkr:SetColor( Color(255,0,0) )
end
end
end
hook.Add("EntityTakeDamage","MunDamage",PlayerDamage)[/lua]
Thanks I appreciate the fast reply. Will test it soon.
This is exactly why I decided to start helping where I can in this forum, to learn new things.
Why should I make my function local instead of global? Is it because there is only one thing referencing it?
[QUOTE=monkstick;41584952]This is exactly why I decided to start helping where I can in this forum, to learn new things.
Why should I make my function local instead of global? Is it because there is only one thing referencing it?[/QUOTE]
You make it local for the sake of not accidentally overwriting a function in a gamemode or addon.
[QUOTE=monkstick;41584952]This is exactly why I decided to start helping where I can in this forum, to learn new things.
Why should I make my function local instead of global? Is it because there is only one thing referencing it?[/QUOTE]
If you create a global function it can be overwritten by any other code that makes a global function with the same name. Making it 'local' keeps the name inside the scope of the current file / function.
If there were 3 or 4 addons with similar code and each one tries to create a global function called PlayerDamage ; whichever one loaded last will overwrite it then they will all call the same function.
EDIT: Ninja'd by brandon. ^
Sorry, you need to Log In to post a reply to this thread.