Prop Count

Is there an way to get the current amout of props that an player got spawned?



hook.Add("PlayerSpawnedProp", "propCounter", function( ply, model, ent )
	ply.spawnedProps = (ply.spawnedProps or 0) + 1
end)


Sorry, need it more complex i need to count an specefic type of prop like

getPropAmout(model)
to check like if getPropAmout(model) = 5 then
print(“You cant spawn theese kind of prop anymore”)

ill hope thats not too much :o

Do you mean like… prop_physics or prop_ragdoll or the model name?

This is for the model:



function GetPropQuantity(sModel)
   local i = 0
   for k,v in ipairs(ents.FindByClass("prop_*")) do
      if v:GetModel() == sModel then
         i = i + 1
      end
   end
return i
end

Whereas the model type would be:


function GetPropQuantity(sType)
   return #ents.FindByClass(sType)
end

Where sType is either prop_physics or prop_ragdoll (or another if you want to be really specific)

Not prop_physics or ragdoll i need to count it by the model of the prop,
Like you spawn 5 chairs then you cant spawn more chairs



 
hook.Add("PlayerSpawnedProp", "propCounter", function( ply, model, ent )
	ent.Owner = ply
end)

function GetPropQuantity(mdl, owner) -- mdl arg is model name while owner arg is the target player
	local props_num = 0
	for k, v in pairs(ents.FindByClass("prop_*")) do
		if v:GetModel()==mdl && v.Owner == owner then 
			props_num = props_num + 1 
		end 
	end
end



Thanks seems like its working :>

[editline]10th August 2015[/editline]

Okay got an error
when i do this :
if not GetPropQuantity(Prop.Model, ply) >= Prop.Limit then

i get :
[Error] attempt to compare string with boolean

[editline]10th August 2015[/editline]

That function does not give me the amout of props it returns me an boolean

[lua]if not tonumber(GetPropQuantity(Prop.Model, ply)) >= tonumber(Prop.Limit) then[/lua]

if you are comparing variables like that, and they end up not being numbers, just use tonumber.

That’s because there should be a brackets like so:


 if not (GetPropQuantity(Prop.Model, ply) >= Prop.Limit) then 

What the code thinks you’re doing is saying if !tonumber for the first part, and comparing !tonumber (which would be false) to the second tonumber for the prop limit.

Also, the GetPropQuantity modification from before doesn’t return anything, it should be like this:


hook.Add("PlayerSpawnedProp", "propCounter", function( ply, model, ent )
	ent.Owner = ply
end)

function GetPropQuantity(mdl, owner) -- mdl arg is model name while owner arg is the target player
	local props_num = 0
	for k, v in pairs(ents.FindByClass("prop_*")) do
		if v:GetModel()==mdl && v.Owner == owner then 
			props_num = props_num + 1 
		end 
	end
        return props_num
end