Tips, hints and how to improve shit

So, this is my second attempt at lua scripting for Gmod. The first one being a music player which I have ceased work on for the time being.

The purpose of this script is to allow me to set individual entity limits per group.



--pLimit v0.1 by Ploo

if not SERVER then return end

Limits = {}
Limits.superadmin = { 
	["prop_physics"] = 10,
	["prop_ragdoll"] = 20 }
Limits.admin = {
	["prop_physics"] = 1000,
	["prop_ragdoll"] = 20 }
Limits.mod = {
	["prop_physics"] = 500,
	["prop_ragdoll"] = 10 }
Limits.donator = {
	["prop_physics"] = 300,
	["prop_ragdoll"] = 5 }
Limits.dj = {
	["prop_physics"] = 250,
	["prop_ragdoll"] = 3 }
Limits.platinum = {
	["prop_physics"] = 200,
	["prop_ragdoll"] = 2 }
Limits.gold = {
	["prop_physics"] = 175,
	["prop_ragdoll"] = 2 }
Limits.silver = {
	["prop_physics"] = 150,
	["prop_ragdoll"] = 1 }
Limits.bronze = {
	["prop_physics"] = 125,
	["prop_ragdoll"] = 1 }
Limits.member = {
	["prop_physics"] = 100,
	["prop_ragdoll"] = 0 }
Limits.user = {
	["prop_physics"] = 50,
	["prop_ragdoll"] = 0 }

function SpawnProp(ply, mdl)
	local class = "prop_physics"
	local count = ply:GetCount("props")
	return AllowSpawn(ply, class, count)
end

function SpawnRagdoll(ply, mdl, ent)
	local class = "prop_ragdoll"
	local count = ply:GetCount("ragdolls")
	return AllowSpawn(ply, class, count)
end

function AllowSpawn(ply, class, count)
	if count >= GetLimit(ply, class) then
		ply:ChatPrint("[pLimit] You have reached your limit of that entity for your group!")
		ply:SendHint("[pLimit] You have reached your limit of that entity for your group!", 10)
		return false
	end
end

function GetLimit(ply, class)
	for k, v in pairs(Limits) do
		if ply:IsUserGroup(k) then
			return v[class]
		end
	end
end

hook.Add( "PlayerSpawnProp", "SpawnProp", SpawnProp )
//hook.Add( "PlayerSpawnSENT", "SpawnSent", SpawnProp )
//hook.Add( "PlayerSpawnSWEP", "SpawnSWEP", SpawnSWEP )
//hook.Add( "PlayerSpawnNPC", "SpawnNPC", SpawnNPC )
//hook.Add( "PlayerSpawnVehicle", "SpawnVehicle", SpawnVehicle )
//hook.Add( "PlayerSpawnEffect", "SpawnEffect", SpawnEffect )
hook.Add( "PlayerSpawnRagdoll", "SpawnRagdoll", SpawnRagdoll )


It works pretty good and I’m happy with it so far but as a beginner coder I know that I have done many things which could be done better, more efficiently, can be optimized or however the fuck else improved.

Also, is there any way of counting the amount of certain entities a player has, by class? I want to count by prop_physics and not props, prop_ragdolls and not ragdolls and sent_ball not sents, if you know what I mean. Not the general group, but the specific class name, although what I got will do for now.

So what I’m looking for, is anything I should know, should learn about, should take into account and stuff. Any contribution is most appreciated.

Functions you put into hooks should be local functions, because they get referenced the same as a
global and globals can produce conflicts.

If you are looking for a way to see x number of props owned by a player, you’ll first need to use a prop ownership system which you can access (or default :GetOwner) and loop through the table with ents.FindByClass and count each entity that is the same owner as the specified player.