reworking illness addon/code

Hi! I recently stumbled upon this script. I am now trying to rework it into a infection script for my private server. My goal for this is when an NPC hits a player, they have a chance of getting infected. Now i’m almost certain I have to use an ontakedamage hook, but im not sure how to implement it. Here is the sv_autorun for it.


include("sh_ill.lua")

local CoughSounds = {"ambient/voices/cough1.wav", "ambient/voices/cough2.wav", "ambient/voices/cough3.wav", "ambient/voices/cough4.wav"}


local function IllCore()
local str, index = table.Random(CoughSounds)
	for k,v in pairs(player.GetAll()) do
		if v:GetNWBool("isill") then
				if v.NextIll == nil then
					v.NextIll = CurTime() + Sick[v:GetNWString("illname")].Rate
					v:SetHealth(v:Health()-Sick[v:GetNWString("illname")].HP)
					v:EmitSound(str)
						if v:Health() <= 0 then
							v:Kill()
							v:SilentCure()
						end
				elseif CurTime() > v.NextIll then
					v.NextIll = CurTime() + Sick[v:GetNWString("illname")].Rate
					v:SetHealth(v:Health()-Sick[v:GetNWString("illname")].HP)
					v:EmitSound(str)
					if v:Health() <= 0 then
							v:Kill()
							v:SilentCure()
					 end
			end	
		end
	end
end
timer.Create( "Timer_Ill_Core", .1, 0, IllCore )
local SickQueue = {}
 
local function InfectTheQueue()
	for k,v in pairs(player.GetAll()) do
		local str, index = table.Random(Sick)
			for o,i in pairs(SickQueue) do
				if v:SteamID() == i then
					v:GiveIll(index) 
				end
			end
		end
	table.Empty(SickQueue)
end
   
function InfectRandomPlayers()
table.Empty(SickQueue)
if #team.GetPlayers(CureMedJob) >= 1 then
	if #player.GetAll() >= ReqMaxPlayersIll then
	for k,v in pairs(player.GetAll()) do
		if table.Count( SickQueue ) != RandomIllPlayers and v:Alive() and not v:isArrested() then
			table.insert( SickQueue, v:SteamID() )
		else 
			return 
		end 
	end
	InfectTheQueue()
	end
	end
end  
timer.Create( "Timer_Pick_Random_Ill", RandomIll, 0, InfectRandomPlayers )  
 
local function SpreadIllnesses()
	for k,v in pairs(player.GetAll()) do
		for o,i in pairs(ents.FindInSphere(v:GetPos(), IllSpreadRadius )) do
			if v:GetNWBool("isill") then
				if i:GetClass() == "player" then
					local illchance = math.random(1,2)
						if illchance == 1 and i:SteamID() != v:SteamID() then
							i:GiveIll(v:GetNWString("illname"))
						end
				end
			end
		end
	end
end
timer.Create( "Spread_Ill_Timer", IllSpreadTime, 0, SpreadIllnesses )

local function CheckMedicCure()
if #team.GetPlayers(CureMedJob) < 1 then 
	for k,v in pairs(player.GetAll()) do
			if v:GetNWBool("isill") then
				v:Cure()
			end
		end
	end
end
hook.Add("OnPlayerChangedTeam", "Ill_Cure_With_No_Medic", CheckMedicCure)
hook.Add("PlayerDisconnected", "Ill_Cure_Medic_Dissconnect", CheckMedicCure)

local function CureOnDeath( victim, inflictor, attacker)
	victim:SilentCure()
end
hook.Add("PlayerDeath", "Player_Cure_On_Death", CureOnDeath) 

here is the sh_ill (shared) code


include("sh_ill.lua")

local CoughSounds = {"ambient/voices/cough1.wav", "ambient/voices/cough2.wav", "ambient/voices/cough3.wav", "ambient/voices/cough4.wav"}


local function IllCore()
local str, index = table.Random(CoughSounds)
	for k,v in pairs(player.GetAll()) do
		if v:GetNWBool("isill") then
				if v.NextIll == nil then
					v.NextIll = CurTime() + Sick[v:GetNWString("illname")].Rate
					v:SetHealth(v:Health()-Sick[v:GetNWString("illname")].HP)
					v:EmitSound(str)
						if v:Health() <= 0 then
							v:Kill()
							v:SilentCure()
						end
				elseif CurTime() > v.NextIll then
					v.NextIll = CurTime() + Sick[v:GetNWString("illname")].Rate
					v:SetHealth(v:Health()-Sick[v:GetNWString("illname")].HP)
					v:EmitSound(str)
					if v:Health() <= 0 then
							v:Kill()
							v:SilentCure()
					 end
			end	
		end
	end
end
timer.Create( "Timer_Ill_Core", .1, 0, IllCore )
local SickQueue = {}
 
local function InfectTheQueue()
	for k,v in pairs(player.GetAll()) do
		local str, index = table.Random(Sick)
			for o,i in pairs(SickQueue) do
				if v:SteamID() == i then
					v:GiveIll(index) 
				end
			end
		end
	table.Empty(SickQueue)
end
   
function InfectRandomPlayers()
table.Empty(SickQueue)
if #team.GetPlayers(CureMedJob) >= 1 then
	if #player.GetAll() >= ReqMaxPlayersIll then
	for k,v in pairs(player.GetAll()) do
		if table.Count( SickQueue ) != RandomIllPlayers and v:Alive() and not v:isArrested() then
			table.insert( SickQueue, v:SteamID() )
		else 
			return 
		end 
	end
	InfectTheQueue()
	end
	end
end  
timer.Create( "Timer_Pick_Random_Ill", RandomIll, 0, InfectRandomPlayers )  
 
local function SpreadIllnesses()
	for k,v in pairs(player.GetAll()) do
		for o,i in pairs(ents.FindInSphere(v:GetPos(), IllSpreadRadius )) do
			if v:GetNWBool("isill") then
				if i:GetClass() == "player" then
					local illchance = math.random(1,2)
						if illchance == 1 and i:SteamID() != v:SteamID() then
							i:GiveIll(v:GetNWString("illname"))
						end
				end
			end
		end
	end
end
timer.Create( "Spread_Ill_Timer", IllSpreadTime, 0, SpreadIllnesses )

local function CheckMedicCure()
if #team.GetPlayers(CureMedJob) < 1 then 
	for k,v in pairs(player.GetAll()) do
			if v:GetNWBool("isill") then
				v:Cure()
			end
		end
	end
end
hook.Add("OnPlayerChangedTeam", "Ill_Cure_With_No_Medic", CheckMedicCure)
hook.Add("PlayerDisconnected", "Ill_Cure_Medic_Dissconnect", CheckMedicCure)

local function CureOnDeath( victim, inflictor, attacker)
	victim:SilentCure()
end
hook.Add("PlayerDeath", "Player_Cure_On_Death", CureOnDeath) 

Any guidance or help would be greatly appreciated!
Thanks!

Well you can start here with this hook. here
Also I’d suggest reading through that thread that you found this in, they explain to the OP that variables should be networked with the net library.

-edit-
so I created something really small that applies effects based on the disease, as well as has a function to print out the status effects that the disease causes to send to players, all you really have to do is just network stuff to the client
and change around a few things to your liking. Not sure how efficient this all is but it works.


--serverside

diseases = {
	["flu"] = {
		["sideeffects"] = { "coughing", "vomiting", "other"},
		["statuseffects"] = function(ply)
			if IsValid(ply) then
				ply:TakeDamage(1)
			end
		end,
	},
};

hook.Add("PlayerInitialSpawn", "setIllnessStat", function(ply)
	ply.sickness = "none";
	ply.isSick = false;
end)

hook.Add("PlayerSpawn", "resetIllness", function(ply)
	ply.sickness = "none";
	ply.isSick = false;
end)

hook.Add("Initialize", "illnessTimer", function()
	timer.Create("checkSickness", 1, 0, function()
		local players = player.GetAll();
		for k, v in pairs(players) do
			if IsValid(v) then
				if v:Alive() then
					v:calcSickness()
					print("updating player on disease")
					print(v.sickness)
				else
					print("currently dead")
				end
			end
		end
	end)
end)

hook.Add("EntityTakeDamage", "infectPlayers", function(target, dmg)
	if target:IsPlayer() and dmg:GetAttacker():IsNPC() or dmg:GetAttacker():IsPlayer() then
		local chanceToInfect = math.random(1, 10); --10 percent chance on npc attack to infect a player
		if chanceToInfect == 1 then
			target:setSickness("flu")
			target:PrintMessage(3, "you've been infected")
		end
	end
end)

local p = FindMetaTable("Player");

function p:calcSickness()
	if self.isSick == true then
		return diseases[self.sickness]["statuseffects"](self)
	end
end

function p:setSickness(name)
	self.sickness = name;
	self.isSick = true;
end

function p:Cure()
	if !self.isSick then print("not sick") return end
	self.sickness = "none";
	self.isSick = false;
end

function p:getSideEffects()
	if !self.isSick then print("not sick") return end
	for k, v in pairs(diseases[self.sickness]["sideeffects"]) do
		print(v)
	end
end