A simple script, eh?

Hey anyone who may be reading this.

A few months ago I had no clue on how to code even a simple timer script, but that changed drastically when I bought from my first GMod server from a GSP that my a friend of mine had
requested.
I started editing the gamemode named DarkRP, ended up making it a ZombieRP. I learnt how to make custom jobs, shipments, entities and such.
I digged in the world of LUA and managed to make an Advertising script;

[lua]advertcycle = 1
timer.Create( “advertimer”, 300, 0, function()
if advertcycle==1 then
chat.AddText(
Color(160,32,240), "[BR] ",
Color(15,221,175), "Respect the rules, play fair, play regularly and you will become a ",
Color(255,128,0), “Regular.”)
chat.PlaySound()
elseif advertcycle==2 then
chat.AddText(
Color(160,32,240), "[BR] ",
Color(15,221,175), "Type /motd in chat to show the ",
Color(255,128,0), “Rules.”)
chat.PlaySound()
elseif advertcycle==3 then
chat.AddText(
Color(160,32,240), "[BR] ",
Color(15,221,175), "Remember to add this server to your ",
Color(255,128,0), “Favourites.”)
chat.PlaySound()
elseif advertcycle==4 then
chat.AddText(
Color(160,32,240), "[BR] ",
Color(15,221,175), "We do play with ",
Color(255,128,0), “NLR”)
Color(15,221,175), “, abide by it!”,
chat.PlaySound()
end
advertcycle = advertcycle + 1
if advertcycle > 4 then
advertcycle = 1
end

end)[/lua]

Okay enough of my ‘Lifestory’ lol…

I have had a few lagg problems on my server due to the high amount of NPC’s - So what I need is a script that sets a maximum of approximately 20 NPC’s.
I know it’s possible, and it sounds fairly easy, but I just can’t figure out how to make it.

[lua]
local entTally = 0;

hook.Add(“EntityRemoved”,“fixTally”,function(e)
if(string.sub(e:GetClass(),1,4)==“npc_”)then
entTally = entTally - 1;
end
end)

hook.Add(“OnEntityCreated”,“removeNPCs”,function(e)
if(string.sub(e:GetClass(),1,4)==“npc_”)then
entTally = entTally + 1;

	if(entTally>20)then
		e:Remove();
	end
end

end)
[/lua]

What happened to sbox_maxnpcs though?

sbox_maxnpcs only limits players spawning NPCs, as this is a gamemode revolving around NPCs I doubt he’s spawning them from his Q menu.

You may say he could just edit the script that’s spawning the NPCs, but I’m guessing from the fact he’s asking this that he’s using DarkRPs built in zombie system, the code for which is a bit of a mess.

Waow, thanks a ton. Would you mind explaining what you did, like step for step?

  • Sorry, I’m still new at this.

It’s a spawnsystem built within the map.

[lua]
local entTally = 0; – this is a variable to represent the number of spawned NPCs

hook.Add(“EntityRemoved”,“fixTally”,function(e) – this is a hook, this function will be called every time an entity is removed
if e:IsNPC() then – if the entity being removed is an NPC
entTally = entTally - 1; – then we subtract 1 from the current NPC count
end
end)

hook.Add(“OnEntityCreated”,“removeNPCs”,function(e) – this is a hook called whenever an entity is created
if e:IsNPC() then – again, check if the entity being created is an NPC
entTally = entTally + 1; – increase the current count of NPCs by 1

	if(entTally>20)then -- if there's more than 20 NPCs
		e:Remove(); -- then we remove it, since it has reached the limit
	end
end

end)
[/lua]

Thanks, I would never have figured that out myself.

  • Most of it was quite obvious though.