Sending net messages from a SWEP?

I get an error whenever I send a net message from my SWEP to the server (for handling). The message works perfectly, but the error really annoys me.

This is my code:



function SWEP:SecondaryAttack()
	local own = self.Owner
	local target = own:GetEyeTrace().Entity

	if not target:IsValid() then return end

	if target:IsFrozen() then
		target:Freeze(false)
		net.Start("launchNotification")
			net.WriteString(target:Nick().." has been un-frozen!")
			net.WriteDouble(0)
		net.Send(own)
        end

end


My error: (I get a server-side AND a client-side error - they look the same)



[ERROR] addons/darkrpmodification/lua/weapons/adminstick/shared.lua:108: attempt to call field 'Send' (a nil value)
  1. unknown - addons/darkrpmodification/lua/weapons/adminstick/shared.lua:108


Any helps is appreciated :smiley:

Someone helped me with a similar problem earlier. Use

Entity:SetNWEntity for SWEPs.

In what context?

WEAPON:SecondaryAttack is Shared
net.Send is Serverside

This means you are trying to call a serverside function on the clientside realm.

To prevent the client from running the code, make sure to surround the server side only code with an if SERVER statement. Example:

[lua]function SWEP:SecondaryAttack()
print( “I’m shared!” )

if CLIENT then
	print( "I'm clientside!" )
end

if SERVER then
	print( "I'm serverside!" )
end

end[/lua]

SecondaryAttack is ran SHARED. You’ll see when it prints the error it is yellow, this means it is a clientside error, and is happening because net.Send only exists serverside. Wrap your sending code inside an if SERVER check.

net.Send is serverside only.

[editline]9th March 2015[/editline]

Ninja’d

That did the trick. Thanks everyone! :smiley: