Knockout function

Is there a way I can call a function in a swep. This is what I have



function KnockoutStart ( ply, command, args, caller ) 
			
				for k,v in pairs(ents.FindInSphere(ply:GetPos(), 30)) do 
					if v:GetClass() == "func_door" then
						Notify(ply, 1, 4, string.format(LANGUAGE.unable, "sleep", "func_door exploit"))
						return ""
					end
				end

				ply.WeaponsForSleep = {}
				for k,v in pairs(ply:GetWeapons( )) do
					ply.WeaponsForSleep[k] = {v:GetClass(), ply:GetAmmoCount(v:GetPrimaryAmmoType()), 
					v:GetPrimaryAmmoType(), ply:GetAmmoCount(v:GetSecondaryAmmoType()), v:GetSecondaryAmmoType(),
					v:Clip1(), v:Clip2()}
					/*{class, ammocount primary, type primary, ammo count secondary, type secondary, clip primary, clip secondary*/
				end
				
	
				local ragdoll = ents.Create("prop_ragdoll")
				ragdoll:SetPos(ply:GetPos())
				ragdoll:SetAngles(Angle(0,ply:GetAngles().Yaw,0))
				ragdoll:SetModel(ply:GetModel())
				ragdoll:Spawn()
				ragdoll:Activate()
				ragdoll:SetVelocity(ply:GetVelocity())
				ragdoll.OwnerINT = ply:EntIndex()
				ply:StripWeapons()
				ply:Spectate(OBS_MODE_CHASE)
				ply:SpectateEntity(ragdoll)
				ply.IsSleeping = true
				ply.SleepRagdoll = ragdoll
				ply.KnockoutTimer = CurTime()
				ply:GetTable().BeforeSleepTeam = ply:Team()
				--Make sure noone can pick it up:
				ragdoll.Owner = ply
				local RP = RecipientFilter()
				RP:RemoveAllPlayers()
				RP:AddPlayer(ply)
				umsg.Start("DarkRPEffects",RP)
					umsg.String("colormod")
					umsg.String("1")
				umsg.End()
				RP:AddAllPlayers()
				Notify(ply,1,4, "You have been knocked out!")
	timer.Create("Knockoutend",55,1, function()
	Notify(ply,1,4, "5")
	end)
	timer.Create("Knockoutend2",56,1, function()
	Notify(ply,1,4, "4")
	end)
	timer.Create("Knockoutend3",57,1, function()
	Notify(ply,1,4, "3")
	end)
	timer.Create("Knockoutend4",58,1, function()
	Notify(ply,1,4, "2")
	end)
	timer.Create("Knockoutend5",59,1, function()
	Notify(ply,1,4, "1")
	end)
	timer.Create("Knockoutend6",60,1, KnockoutEnd,ply)
	end
AddChatCommand("knockout", KnockoutStart)
function KnockoutEnd(ply, command, args, caller)

	    ply.OldHunger = ply.DarkRPVars.Energy
		local ragdoll = ply.SleepRagdoll
		local health = ply:Health()
		ply:Spawn()
		ply:SetHealth(health)
		ply:SetPos(ragdoll:GetPos())
		ply:SetAngles(Angle(0, ragdoll:GetPhysicsObjectNum(10):GetAngles().Yaw, 0))
		ply:UnSpectate()
		ply:StripWeapons()
		ply:Spectate(OBS_MODE_NONE)
		ply:SetMoveType(MOVETYPE_WALK)
		ragdoll:Remove()
				if ply.WeaponsForSleep and ply:GetTable().BeforeSleepTeam == ply:Team() then
					for k,v in pairs(ply.WeaponsForSleep) do
						local wep = ply:Give(v[1])
						ply:RemoveAllAmmo()
						ply:SetAmmo(v[2], v[3], false)
						ply:SetAmmo(v[4], v[5], false)
						
						wep:SetClip1(v[6])
						wep:SetClip2(v[7])
						
					end
				
					local cl_defaultweapon = ply:GetInfo( "cl_defaultweapon" )
					if ( ply:HasWeapon( cl_defaultweapon )  ) then
						ply:SelectWeapon( cl_defaultweapon ) 
					end
					ply:GetTable().BeforeSleepTeam = nil
				else
					GAMEMODE:PlayerLoadout(ply)
				
            			
				ply.WeaponsForSleep = {}
				local RP = RecipientFilter()
				RP:RemoveAllPlayers()
				RP:AddPlayer(ply)
				umsg.Start("DarkRPEffects", RP)
					umsg.String("colormod")
					umsg.String("0")
				umsg.End()
				RP:AddAllPlayers()
				if command == true then
					ply:Arrest()
				end
				ply.Sleeping = false
				ply:SetDarkRPVar("Energy", ply.OldHunger)
				ply.OldHunger = nil
				
				if ply.DarkRPVars.Arrested then
					GAMEMODE:SetPlayerSpeed(ply, GetConVarNumber("aspd"), GetConVarNumber("aspd") )
				end
			 end
	      end
	   
    
	
	
			
local function DamageSleepers(ent, inflictor, attacker, amount, dmginfo)
	local ownerint = ent.OwnerINT
	if ownerint and ownerint ~= 0 then
		for k,v in pairs(ply.GetAll()) do 
			if v:EntIndex() == ownerint then
				if attacker == GetWorldEntity() then
					amount = 10
					dmginfo:ScaleDamage(0.1)
				end
				v:SetHealth(v:Health() - amount)
				if v:Health() <= 0 and v:Alive() then
					v:Spawn()
					v:UnSpectate()
					v:SetPos(ent:GetPos())
					v:SetHealth(1)
					v:TakeDamage(1, inflictor, attacker)
					ent:Remove()
				end
			end
		end
	end
end
hook.Add("EntityTakeDamage", "Sleepdamage", DamageSleepers)


Now would there be a way to make it so when a weapon attacks the trace.entity it will call this function

Arent you missing some parenthesis in the third line? Not sure if it’s giving you trouble, but shouldnt it read:

for k, v in pairs(ents.FindInSphere(ply:GetPos()))

Yeah, that is messed up but I still need a way for a swep to use this…

I tested the function and it worked.