Custom Reload Function

It’s really simple. This is the reload function for my healthkit SWEP.
[lua]
function SWEP:Reload()
local trace=(self.Owner:GetEyeTrace())
if (trace.HitPos:Distance(self.Owner:GetShootPos())<=45) then
local ent=(self.Owner:GetEyeTrace().Entity)
if ((ent:IsValid())and((ent:IsPlayer())or(ent:IsNPC()))) then
local current=(ent:Health())
local max=(ent:GetMaxHealth())
local percentage=(math.Round((current/max)*100))
self.Owner:PrintMessage(HUD_PRINTTALK, “Scans indicate this individual is " … percentage … " percent healthy.”)
end
end
end
[/lua]
However, when you press the reload key, it spams the chatbox with the message. I want it to only send one message per press of the key. Is this even possible with reload?

SWEP:Reload() fires continually when key is pressed. Try using Gamemode.KeyPress instead

like this.



function KeyPress(self.Owner, IN_RELOAD)  

local trace=(self.Owner:GetEyeTrace())  

if (trace.HitPos:Distance(self.Owner:GetShootPos())<=45) then  
         
local ent=(self.Owner:GetEyeTrace().Entity)  
         if ((ent:IsValid())and((ent:IsPlayer())or(ent:IsNPC()))) then  
             local current=(ent:Health())  
             local max=(ent:GetMaxHealth())  
             local percentage=(math.Round((current/max)*100))  
             self.Owner:PrintMessage(HUD_PRINTTALK, "Scans indicate this individual is " .. percentage .. " percent healthy.")  
         end  
   end  
 end


I followed your advice and put this in, but it breaks the swep. Help?

[lua]
function SWEP:MedicalScan(self.Owner,IN_RELOAD)
local trace=(self.Owner:GetEyeTrace())
if (trace.HitPos:Distance(self.Owner:GetShootPos())<=45) then
local ent=(self.Owner:GetEyeTrace().Entity)
if ((ent:IsValid())and((ent:IsPlayer())or(ent:IsNPC()))) then
local current=(ent:Health())
local max=(ent:GetMaxHealth())
local percentage=(math.Round((current/max)*100))
self.Owner:PrintMessage(HUD_PRINTTALK, “Scans indicate this individual is " … percentage … " percent healthy.”)
end
end
end

hook.Add(“KeyPress”,“MedicalScan”,MedicalScan)
[/lua]

I tried it multiple times. It breaks whether the SWEP part of SWEP:MedicalScan() is in the function name or not.

[lua]
function SWEP:MedicalScan(self.Owner,IN_RELOAD)
if self.Owner:KeyDownLast(IN_RELOAD) then return end
local trace=(self.Owner:GetEyeTrace())
if (trace.HitPos:Distance(self.Owner:GetShootPos())<=45) then
local ent=(self.Owner:GetEyeTrace().Entity)
if ((ent:IsValid())and((ent:IsPlayer())or(ent:IsNPC()))) then
local current=(ent:Health())
local max=(ent:GetMaxHealth())
local percentage=(math.Round((current/max)*100))
self.Owner:PrintMessage(HUD_PRINTTALK, “Scans indicate this individual is " … percentage … " percent healthy.”)
end
end
end
[/lua]

This should work.

Um, I put that in and now it’s broken again.

Specifically, the SWEP is now a pistol that shoots AR2 rounds with shotgun rounds as its secondary and has a messed up ammo counter, which, in my experience, is what it gives you when you request a broken SWEP.

Do I need to hook it?

EDIT:

Oh I see what you’re saying. Adding that single line makes the reload function only execute once.
I got it working, by the way.