SWEP with less damage at far distances (LUA Error)
13 replies, posted
I Have been working on a shotgun that damage gets reduced when the distance between victim and attacker is too high, I tried that with this code:
[code]
function GM:ScalePlayerDamage( victim, dmginfo )
local attacker = dmginfo:GetAttacker
local lenght = (victim:GetPos():Distance(attacker:GetPos())
if lenght >= 1000 then
dmginfo:ScaleDamage( 0 )
end
if lenght >= 800 and <= 1000 then
dmginfo:ScaleDamage( 0.25 )
end
if lenght >= 600 and <= 800 then
dmginfo:ScaleDamage( 0.5 )
end
if lenght >= 400 and <= 600 then
dmginfo:ScaleDamage( 0.75 )
end
if lenght >= 200 and <= 400 then
dmginfo:ScaleDamage( 0.9 )
end
end
[/code]
but when testing the SWEP it comes this error:
[code]
[ERROR] addons/insurgency/lua/weapons/wep_ins_toz.lua:285: function arguments expected near 'local'
1. unknown - addons/insurgency/lua/weapons/wep_ins_toz.lua:0
[/code]
(In this case line 285 is line 4 of the Code)
When solving it please don't only write the right code, I want to know what I was doing wrong.
[code]local attacker = dmginfo:GetAttacker
local lenght = (victim:GetPos():Distance(attacker:GetPos())[/code]
You're missing parentheses after GetAttacker and you have an extra parenthesis before victim.
Thank you for the Quick reply,
The Error is fixed but i got a new error
[code]
[ERROR] addons/insurgency/lua/weapons/wep_ins_toz.lua:282: attempt to index global 'GM' (a nil value)
1. unknown - addons/insurgency/lua/weapons/wep_ins_toz.lua:282
[/code]
(line 282 = 1)
[QUOTE=Thookie;51953747]Thank you for the Quick reply,
The Error is fixed but i got a new error
[code]
[ERROR] addons/insurgency/lua/weapons/wep_ins_toz.lua:282: attempt to index global 'GM' (a nil value)
1. unknown - addons/insurgency/lua/weapons/wep_ins_toz.lua:282
[/code]
(line 282 = 1)[/QUOTE]
Use GAMEMODE if it isn't a core gamemode file. Only use GM if it is a core gamemode file.
I think it's because using GM functions doesn't work weapon scripts
Untested workaround :
[code]function SWEP:PrimaryAttack()
local tent = self.Owner:GetEyeTrace()
if(self.Owner:GetPos():Distance(tent.Entity:GetPos()) >= 1000) then
self.Primary.Damage = 30
end
shootbullet
end[/code]
[editline]13th March 2017[/editline]
Rip automerge
[QUOTE=bilbasio;51953816]Untested workaround :
[code]function SWEP:PrimaryAttack()
local tent = self.Owner:GetEyeTrace()
if(self.Owner:GetPos():Distance(tent.Entity:GetPos()) >= 1000) then
self.Primary.Damage = 30
end
shootbullet
end[/code]
[editline]13th March 2017[/editline]
Rip automerge[/QUOTE]
You'll gave to be sure to reset the damage to its previous value
I took the version of txike and it works now, thank you all much :*
Just if someone else wants to see how the code looks now:
[code]
function GAMEMODE:EntityTakeDamage( victim, dmginfo )
local attacker = dmginfo:GetAttacker()
local lenght = (victim:GetPos()):Distance(attacker:GetPos())
if lenght >= 1000 then
dmginfo:ScaleDamage( 0 )
end
if lenght >= 800 and lenght <= 1000 then
dmginfo:ScaleDamage( 0.25 )
end
if lenght >= 600 and lenght <= 800 then
dmginfo:ScaleDamage( 0.5 )
end
if lenght >= 400 and lenght <= 600 then
dmginfo:ScaleDamage( 0.75 )
end
if lenght >= 200 and lenght <= 400 then
dmginfo:ScaleDamage( 0.9 )
end
end
[/code]
Ok it works, but for all weapons...
If this is for an addon and not a gamemode, use a hook, instead.
[QUOTE=code_gs;51954072]If this is for an addon and not a gamemode, use a hook, instead.[/QUOTE]
Ok, how do i use the hook right (i am not that much experienced in Lua)
[QUOTE=Thookie;51954112]Ok, how do i use the hook right (i am not that much experienced in Lua)[/QUOTE]
[url]https://wiki.garrysmod.com/page/hook/Add[/url]
Refer to the examples.
Did you meaned this way?
[code]
local function LessDamageOnFar( victim, dmginfo )
local attacker = dmginfo:GetAttacker()
local lenght = (victim:GetPos()):Distance(attacker:GetPos())
if lenght >= 1000 then
dmginfo:ScaleDamage( 0 )
end
if lenght >= 800 and lenght <= 1000 then
dmginfo:ScaleDamage( 0.25 )
end
if lenght >= 600 and lenght <= 800 then
dmginfo:ScaleDamage( 0.5 )
end
if lenght >= 400 and lenght <= 600 then
dmginfo:ScaleDamage( 0.75 )
end
if lenght >= 200 and lenght <= 400 then
dmginfo:ScaleDamage( 0.9 )
end
end
hook.Add( "Think", "LessDamageOnFar ", LessDamageOnFar )
[/code]
because so I get this error:
[code]
[ERROR] addons/insurgency/lua/weapons/wep_ins_toz.lua:284: attempt to index local 'dmginfo' (a nil value)
1. v - addons/insurgency/lua/weapons/wep_ins_toz.lua:284
2. unknown - lua/includes/modules/hook.lua:84
[/code]
Don't use Think, use the function name that you had before for the hook event.
It Where again for all weapons, I looked a bit around and now I am using this script:
[code]
function GAMEMODE:EntityTakeDamage( victim, dmginfo )
local attacker = dmginfo:GetAttacker()
local lenght = (victim:GetPos()):Distance(attacker:GetPos())
local wep = attacker:GetActiveWeapon()
if lenght >= 1000 and wep:GetClass() == "wep_ins_toz" then
dmginfo:ScaleDamage( 0 )
end
if lenght >= 800 and lenght <= 1000 and wep:GetClass() == "wep_ins_toz" then
dmginfo:ScaleDamage( 0.25 )
end
if lenght >= 600 and lenght <= 800 and wep:GetClass() == "wep_ins_toz" then
dmginfo:ScaleDamage( 0.5 )
end
if lenght >= 400 and lenght <= 600 and wep:GetClass() == "wep_ins_toz" then
dmginfo:ScaleDamage( 0.75 )
end
if lenght >= 200 and lenght <= 400 and wep:GetClass() == "wep_ins_toz" then
dmginfo:ScaleDamage( 0.9 )
end
end
[/code]
The Script works totaly fine and I am happy now thanks for everyone who helpen me.
Sorry, you need to Log In to post a reply to this thread.