BodyArmor Hook?

Hey All, Ive got a weird error from a function which I made that says it dont know what ‘Armor’ Is, Yet the gmodwiki tells me its correct, Any help much Appreciated.

[lua]

function Bodyarmour(ply)
local plys = ents.FindByClass(“player”)
if (plys:Armor() > 0) then
local hat = ents.Create(“ba_chest”);
hat:SetOwner(plys);
elseif (plys:Armor( 0 )) then
ents.remove(“ba_chest”);
end
end
hook.Add(“Think”, “Bodyarmour”, Bodyarmour)

[/lua]

Basically what I am trying to achieve, is it will spawn a entity on the player when the player has armour/suit armour.

Any help appreciated.

ents.FindByClass(“player”) returns a table of players. Your function knowledge seems to be lacking.

Also, beware of what you’re trying to do - every frame, you’ll be creating an entity on each player.

[editline]10:10AM[/editline]

[lua]

function Bodyarmour()
for _, v in pairs( player.GetAll() ) do
if (v:Armor() > 0 and not v.ArmorEnt) then
v.ArmorEnt = ents.Create(“ba_chest”);
v.ArmorEnt:SetOwner(v); – assuming that ba_chest defines pos, model etc
v.ArmorEnt:Spawn();
elseif( v:Armor() == 0 ) then
v.ArmorEnt:Remove()
v.ArmorEnt = nil;
end
end
end
hook.Add(“Think”, “Bodyarmour”, Bodyarmour)

[/lua]

Untested

Yeah I was thinking about that, Is this anything you would suggest?

Checking if a player had taken damage and applying armor then.

Testing will post results.

[editline]03:55PM[/editline]

Hook ‘Bodyarmour’ Failed: test\gamemode\init.lua:152: attempt to index field ‘ArmorEnt’ (a nil value)

Thats what I got.

Oh wait I think you forgot to set it as a local value or am I completely worng?

Any Help anyone?

We need to know which line in that code snippet is line 152 in the whole script.

Ops sorry about that

[lua]

function Bodyarmour()
for _, v in pairs( player.GetAll() ) do
if (v:Armor() > 0 and not v.ArmorEnt) then
v.ArmorEnt = ents.Create(“ba_chest”);
v.ArmorEnt:SetOwner(v);
v.ArmorEnt:Spawn();
elseif( v:Armor() == 0 ) then
v.ArmorEnt:Remove() ---- Line giving the error
v.ArmorEnt = nil;
end
end
end
hook.Add(“Think”, “Bodyarmour”, Bodyarmour)

[/lua]

if v:Armor() == 0 and v.ArmorEnt then

Ok thanks, Testing…