Problem with models that are in the map.

I am planning on “chopping” down the trees in the map with an axe.

I have this code and it wont even detect the trees as entities so i tried replacing all the trees in the map with the code below but still nothing.

Could anyone help me with my tree chopping problems.

Replace all trees
[lua]hook.Add(“InitPostEntity”, “TreeReset”, function()
print(“REDO TREE HOOK”)
for k,v in pairs(ents.GetAll()) do
if v:GetModel() then
if v:IsTree() or string.find(v:GetModel(), “tree”) then
local tree =ents.Create(“prop_physics”)
tree:SetPos(v:GetPos())
tree:SetModel(v:GetModel())
tree:SetAngles(v:GetAngles())
tree:Spawn()
v:Remove()
print(“Tree Removed And Replanted”)
end
end
end
end)[/lua]

Axe Attack
[lua]function SWEP:PrimaryAttack()
print(“attacked”)
if CLIENT then return end
self.Weapon:SetNextPrimaryFire(CurTime() + 2)
self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)
self.Owner:EmitSound(Sound(“weapons/iceaxe/iceaxe_swing1.wav”))

local trace = {}
trace.start = self.Owner:GetShootPos()
trace.endpos = trace.start + (self.Owner:GetAimVector() * 80)
trace.filter = self.Owner

local tr = util.TraceLine(trace)
if !tr.HitNonWorld then return end
if !tr.Entity then return end
print("traced and checked")
if tr.Entity:IsTree() then
	print("is tree")
    local data = {}
    data.Entity = tr.Entity
	data.Chance = 60
    data.MinAmount = 1
    data.MaxAmount = 4
    self.Owner:ChopTree(2,data)
end
print("done all")

end[/lua]

IsTree function
[lua]local trees = {“models/props/de_inferno/tree_large.mdl”,“models/props/de_inferno/tree_small.mdl”,“models/props_foliage/tree_deciduous_03a.mdl”,“models/props_foliage/tree_deciduous_01a.mdl”,“models/props_foliage/oak_tree01.mdl”,“models/props_foliage/tree_cliff_01a.mdl”,
“models/props_foliage/tree_deciduous_01a-lod.mdl”,“models/props_foliage/tree_deciduous_02a.mdl”}

function emeta:IsTree()
for k,v in pairs(trees) do
if string.lower(v) == self:GetModel() or string.gsub(string.lower(v),"/","\") == self:GetModel() then
return true
end
end

return false

end[/lua]

So could some please help me, i cant get the trees to be picked up as entities and they need replacing or at least detecting.

prop_multiplayer_physics don’t count as entity’s usually, I’m not sure if you can remove them.

Well thats a piss take then.

I wonder if im going to just make seperate trees then.

And also this MAY not be the issue, but look at line 9 and 11 on the IsTree function. It’s returning false no matter what the outcome of the if statement because it is ran afterwards.

Maybe I’m thinking you’re trying to do something else?

There’s no issue with that.

When you return a value it breaks the the function.
So if it gets the tree then it returns true and stops there.
Also if it doesn’t find a tree it skips over the if function and breaks the function when it returns false.

Oh that’s right… my bad :stuck_out_tongue: