net.SendToServer nil value

This is the code I am calling clientside to send the server the integer.



AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua") 

include('shared.lua')

if SERVER then
	ENT.cratesAdded = 0
end

function ENT:Initialize() self:SetModel("models/Items/item_item_crate.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) self:SetUseType(SIMPLE_USE) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end end

function ENT:Touch(ent)
	if ent.IsVehicle() then
		self:EmitSound( "items/ammocrate_close.wav", 75, 100, 1, CHAN_AUTO )
		self:Remove()
		print("crate added")
		net.Start( "cratesAdd" )
			net.WriteUInt( self.cratesAdded + 1, 8 ) -- You can also use net.WriteInt here. The second argument is 8 since the age will never be above 255. Doing this saves bandwidth and can reduce lag.
		net.SendToServer()
	end
end


and my serverside which tries to get the cratesAdded



util.AddNetworkString( "cratesAdd" )

net.Receive( "cratesAdd", function()
		print("Received message! You have added "..cratesAdded.. " crates!")
end)


and this is the error I get…



[ERROR] addons/citizenjobs/lua/entities/crate/init.lua:19: attempt to call field 'SendToServer' (a nil value)
  1. unknown - addons/citizenjobs/lua/entities/crate/init.lua:19


ENT:Touch() is a serverside only callback.
net.SendToServer() is clientside only so the server doesn’t have it.
You don’t even have to send anything from client to server since the server knows it touched by itself.

Ahh I see where I went wrong. I am calling net.SendToServer on serverside so its trying to send it to itself. Should I use broadcast instead?

no don’t use net library at all, just a simple function call

[lua]function ENT:Touch(ent)
if ent.IsVehicle() then
self:EmitSound( “items/ammocrate_close.wav”, 75, 100, 1, CHAN_AUTO )
self:Remove()
print(“crate added”)
addCrate() – or whatever
end
end[/lua]