Finding a prop_physics by model using ents.FindInSphere
8 replies, posted
Hi guys, so I'm trying to find a way to remove a prop near the player if it has a certain model, but I'm not sure how to do it.
After messing around here is my code:
[LUA]local killents = ents.FindInSphere( ply:GetPos(), 300 )
//PrintTable(killents)
//table.HasValue( killents, "prop_physics" )
for k, v in pairs( killents ) do
if v:GetModel() == killmodels then
ply:UnSpectate()
ply:Spawn()
ply:SetPos( v:GetPos() )
PrintTable(v)[/LUA]
If anyone could shed some insight I'd love you forever.
Can you show us what killmodels is?
just a table of models
[LUA]local killmodels = {
"models/props_junk/cardboard_box001b.mdl",
"models/props_junk/TrashBin01a.mdl",
"models/props_wasteland/barricade001a.mdl",
"models/props_junk/TrafficCone001a.mdl",
"models/props/CS_militia/dryer.mdl",
"models/props/cs_office/trash_can.mdl",
"models/props_c17/oildrum001.mdl",
"models/props_interiors/Furniture_chair03a.mdl",
}[/LUA]
Ok, well you can't compare strings to tables.
[lua]
v:GetModel() == killmodels
[/lua]
So what you want to do is actually use table.HasValue here. Check to see if killmodels has v:GetModel() in it and then do your stuff.
I'm not at home right now, but could you do
[LUA]if table.HasValue( v, killmodels ) then
v[1]:Remove()[/LUA]
?
Yes, but make sure you are getting the model. And you also don't need to index v. It is an entity, not a table. Oh and table.HasValue's first arg should be the table, not the value.
I fixed it for you.
[lua]
if table.HasValue( killmodels, v:GetModel() ) then
v:Remove()
end
[/lua]
Thanks!
I just thought it would be good to index v in case there is more than one of the prop :v:
[QUOTE=rejax;41924064]Thanks!
I just thought it would be good to index v in case there is more than one of the prop :v:[/QUOTE]
When you loop through the table it doesn't return any sub-tables, it returns single entities. This means it will loop 20 times through 20 identical objects if they are within the ents.FindInSphere()
If the table had been a table of tables this still wouldn't have been right as you would have needed to loop through that table to get information about each of it's members too...
[QUOTE=rejax;41924064]Thanks!
I just thought it would be good to index v in case there is more than one of the prop :v:[/QUOTE]
Well, this should still be in your loop. The loop will take care of every prop.
To save you the trouble, here's the whole thing.
[lua]
local killents = ents.FindInSphere( ply:GetPos(), 300 )
for k, v in pairs( killents ) do
if table.HasValue( killmodels, v:GetModel() ) then
v:Remove()
end
end
[/lua]
Sorry, you need to Log In to post a reply to this thread.