SWEP vehicle health

Hey there,

I’ve been busy for weeks and came with the idea of making a vehicle repair swep, since my addon has an implemented damage system.
So I started but it’s not working, and the code’s probaply not making any sense.



-- Variables
defaulthp = 150		-- Vehicle default health
 
AddCSLuaFile ("weapon_dmodrepairtool.lua")
 
SWEP.Weight = 5
 
SWEP.AutoSwitchTo = true
SWEP.AutoSwitchFrom = true

SWEP.PrintName				= "Vehicle Repair Tool"
SWEP.Author					= "IAmCalledKoenians"
SWEP.Purpose				= "Repairs the broken down vehicle."

SWEP.Slot					= 1
SWEP.SlotPos				= 3

SWEP.Spawnable				= true

SWEP.ViewModel				= Model( "models/wrepons/v_crowbar.mdl" )
SWEP.WorldModel				= Model( "models/wrepons/w_crowbar.mdl" )
SWEP.ViewModelFOV			= 54
SWEP.UseHands				= true
SWEP.Category				= "DrivingMOD"

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= true
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo			= "none"

SWEP.DrawAmmo				= false
SWEP.HoldType				= "Melee"

local ShootSound = Sound("Metal.SawbladeStick")

function SWEP:Reload()
end
 
function SWEP:Think()
end
  
function SWEP:PrimaryAttack()
	local tr = self.Owner:GetEyeTrace()
		
	self.Owner:GetPos(self.Owner:EyePos() + (self.Owner:GetAimVector() * 16))
	self.Owner:SetAngles(self.Owner:EyeAngles())

	
	ent:SetHealth(ent:Health() + ( defaulthp ))
	self:EmitSound(ShootSound)
	self.BaseClass.ShootEffects(self)
	for k, v in pairs( player.GetAll( ) ) do
		v:SendLua("GAMEMODE:AddNotify(\"You fixed the broken car!\", NOTIFY_GENERIC, 3)")
	end
end

 
function SWEP:SecondaryAttack()
end
 
 


How do I make it work the way it should?

Kind regards,

Koen

Instead of using ‘ent’, try using tr.Entity (that’d be the entity returned by the trace)

[editline]7th May 2016[/editline]

E.g.



local tr = self.Owner:GetEyeTrace()
	
self.Owner:GetPos(self.Owner:EyePos() + (self.Owner:GetAimVector() * 16)) -- not sure why you want to do this stuff but okay then
self.Owner:SetAngles(self.Owner:EyeAngles())

	
tr.Entity:SetHealth(tr.Entity:Health() + ( defaulthp ))


[editline]7th May 2016[/editline]

Also, please change your defaulthp variable so it’s SWEP.defaulthp (sorry, I just hate global variables)

It’s not giving any errors right now, but it’s not setting the vehicle it’s health to 150.

I’m not quite sure, but I checked and it seems SetHealth only works on players