Really cool module, though you use networked variables in a lot of places that you.. well, don't need to be networking things
I would advise going through your script and checking to make sure that every single NWVar you use has to be access by everyone on the map. If you don't need everyone on the server to be aware of an NWVar, don't use an NWVar.
Also, don't use SendLua so much.
Use usermessages, they're a lot more reliable.
SendLua has a limit to how much data can be transferred per message and sometimes the data will be dropped or cut off at a certain character limit and the client will end up executing an unfinished script.
Other than that, it's a really cool mod, good job :)
Flawless just pointed out that you use Think hooks a lot.. and, well, really poorly actually
You have 3 functions hooked to "Think", which is a bad start.
Think is called each tick, and you're running it on the server, so you'll be lagging the server quite a bit, mostly because in each of the three Think hooks you're calling, you're looping through all the players in the map, which means each tick of the server, you're making the server loop through the players and perform various actions, including modifying networked data, 3 times every tick, which is bad.
In CM.ChangedTeam() you loop through each player on the server (Once again in the Think hook), and then inside an if-statement you're looping through each player AGAIN, in the middle of an ongoing for-loop through each player in the server. Essentially, it's possible that you're looping through every player in the server 3-4 times each tick, PLUS doing an additional loop for every player on the server. That's a lot of loops
function CM.ChangedTeam() -- Somehow the default detect team functions didnt want to co-operate :( So I made this crappy workaround.
for k,user in pairs(player.GetAll()) do
if user:Team() == TEAM_MOPERATOR and not user:GetNWBool("cm_config") then
elseif user:Team() != TEAM_MOPERATOR and user:GetNWString("cm_company") == user:GetNWString("cm_operator") and user:GetNWString("cm_company") != nil and user:GetNWString("cm_company") != "" and user:GetNWString("cm_operator") != nil and user:GetNWString("cm_operator") != "" then
for k, v in pairs(player.GetAll()) do
if v:GetNWString("cm_operator") == user:GetNWString("cm_company") then
v:SendLua("chat.AddText(Color(255,50,0), 'Your cellphone operator "..user:GetNWString("cm_company").." has been dissolved')")
v:SendLua("chat.AddText(Color(255,50,0), 'Your current simcard will still work untill you change.')")
elseif user:Team() != TEAM_MOPERATOR then
hook.Add("Think", "CM.ChangedTeam", CM.ChangedTeam)
Using the think hook is fine, you just need to know how to control your intervals.
Create a single function hooked to Think, and put all of your current Think hook logic inside that function, then make a variable named "cm_NextTick" and use that to delay the execution of your code, er, here lemme show you:
local cm_NextTick = CurTime()
local cm_TickDelay = 3
if CurTime() >= cm_NextTick then --If it's time to run our tick..
--Run the code
cm_NextTick = CurTime() + cm_TickDelay --Delay the next tick
hook.Add("Think", "CMThink", CM.Think)
These are all honest mistakes and nobody should be railing on you for making them, but you should definitely fix these issues if you want people to use this addon on their servers.