Cuttable Tree Problem

I am trying to make a cuttable tree, but i have a problem.
If a tree regrows, it resets the health of all the other trees.
I need to make it so that each tree has its own health.

[lua]
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )
include(‘shared.lua’)

local healthpoints = 100

function ENT:Initialize( )
self:SetModel( “models/props_foliage/tree_deciduous_01a.mdl” )
self:SetUseType( SIMPLE_USE )
self:SetSolid( SOLID_VPHYSICS )
end

function ENT:SpawnFunction( ply, tr )

if ( !tr.Hit ) then return end

local ent = ents.Create( "resource_tree" )
	ent:SetPos( tr.HitPos ) 
	ent:Spawn()

return ent

end

function ENT:OnTakeDamage( dmginfo )
local dmg = dmginfo:GetDamage()
local attacker = dmginfo:GetAttacker()

if attacker:GetActiveWeapon():GetClass() == "weapon_crowbar" then
	healthpoints = healthpoints - dmg
end
if healthpoints < 1 then
	attacker:ChatPrint("Tree Cut Down!")
	self:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl")
	healthpoints = 100
	timer.Create("T"..math.random(1,100) * math.random(1,100) * math.random(1,100).."T", 10, 1, function()
		self:SetModel( "models/props_foliage/tree_deciduous_01a.mdl" )
		
	end)
end

end
[/lua]

Any help appreciated thanks :slight_smile:

Make your own little table entry for the entity:

ent.treehealth = 100

Same trick is used to set prop owners.

[editline]9th February 2012[/editline]

This would mean, of course, not using a global for the value. Use the ent:Initialize() part of the script to set the default ent.treehealth table value.

How would I do that?
Im not too good at lua sorry :frowning:

Uh, one sec.

[editline]9th February 2012[/editline]

Like this: (I think, anyway)

[lua]
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )
include(‘shared.lua’)

– No globals.
– local healthpoints = 100

function ENT:Initialize( )
self:SetModel( “models/props_foliage/tree_deciduous_01a.mdl” )
self:SetUseType( SIMPLE_USE )
self:SetSolid( SOLID_VPHYSICS )
self.treehealth = 100
end

function ENT:SpawnFunction( ply, tr )

if ( !tr.Hit ) then return end
 
local ent = ents.Create( "resource_tree" )
    ent:SetPos( tr.HitPos ) 
    ent:Spawn()

return ent

end

function ENT:OnTakeDamage( dmginfo )
local dmg = dmginfo:GetDamage()
local attacker = dmginfo:GetAttacker()

if attacker:GetActiveWeapon():GetClass() == "weapon_crowbar" then
    self.treehealth = self.treehealth - dmg
end
if self.treehealth < 1 then
    attacker:ChatPrint("Tree Cut Down!")
    self:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl")
    self.treehealth = 100
    timer.Create("T"..math.random(1,100) * math.random(1,100) * math.random(1,100).."T", 10, 1, function()
        self:SetModel( "models/props_foliage/tree_deciduous_01a.mdl" )
         
    end)
end

end
[/lua]

[editline]9th February 2012[/editline]

I’m also going to bed. If I’m totally wrong (very probable), I’m sure someone else knows what you’re doing.