Need help making my code abide by prop limit var

I have been editing a gamemode to allow players to spawn props. But for whatever reason, it seems players can spawn unlimited amounts of props. It’s as if it is bypassing the server’s prop limit ( sbox_maxprops 30 ). Before it is asked, yes my gamemode derives from sandbox. The function below is called when the props ‘icon’ is clicked in the gamemode’s menu. A snippet of my spawn prop function is below…


function SpawnProp(ply,cmd,args)
	if !ply:IsValid() or !args[1] or args[2] then return end
	local model = args[1]:Trim()
	local prop = PROPS[model]
	if !prop then return end

	local tr = ply:GetEyeTraceNoCursor()
        local ent = ents.Create ("prop_physics")
	ent:SetModel(model)
	ent:SetPos(tr.HitPos + Vector( 0, 0, 10 ))
	ent:Spawn()
	ent:CPPISetOwner(ply)
		
	hook.Call("PlayerSpawnedProp", gmod.GetGamemode(), ply, ent:GetModel(), ent)
end
concommand.Add("SpawnProp", SpawnProp)

So my question, what am I missing to make it abide by the server’s prop limit? or what extra needs to be done? Thanks for any help in advanced.

I think you need to loop through all props and count all props that belong to the ply, then if that’s > sbox_maxprops then return false.

[lua]
local canSProp = ply:GetCount(“props”) < GetConVar(“sbox_maxprops”):GetInt()
[/lua]


function SpawnProp(ply,cmd,args)
if ply:GetCount("props") < GetConVar("sbox_maxprops"):GetInt() then
	if !ply:IsValid() or !args[1] or args[2] then return end
	local model = args[1]:Trim()
	local prop = PROPS[model]
	if !prop then return end

	local tr = ply:GetEyeTraceNoCursor()
        local ent = ents.Create ("prop_physics")
	ent:SetModel(model)
	ent:SetPos(tr.HitPos + Vector( 0, 0, 10 ))
	ent:Spawn()
	ent:CPPISetOwner(ply)
		
	hook.Call("PlayerSpawnedProp", gmod.GetGamemode(), ply, ent:GetModel(), ent)
else
       ply:SendLua("GAMEMODE:AddNotify(\"You have reached the prop limit!.\", NOTIFY_ERROR, 4)")
end
end
concommand.Add("SpawnProp", SpawnProp)

Awesome! Thank you guys!