networking problem

Hello, i am having a problem with my code. I forked a bit of the drug lab code and made a different. net.SendToServer and net.Receive doesn’t work for me. Could anyone help me?

INIT.LUA



AddCSLuaFile("cl_init.lua");

util.AddNetworkString("druglab_toserver")

include("shared.lua")

DEFINE_BASECLASS("lab_base")

ENT.SeizeReward = 350

function ENT:Initialize()
    BaseClass.Initialize(self)
	self.SID = self:Getowning_ent().SID
end

function ENT:Use( ply, caller )
	umsg.Start( "DrawTheMenu", ply )
		umsg.Short( "1" )
	umsg.End()
end



net.Receive( "druglab_toserver", ENT:createItem )

function ENT:createItem()
    local drugType = net.ReadString()
    local drugPos = self:GetPos()
    local drug = ents.Create(drugType)
    drug:SetPos(Vector(drugPos.x, drugPos.y, drugPos.z + 35))
    drug.nodupe = true
    drug:Spawn()
end


CL_INIT



include("shared.lua")

function ENT:Draw()
self.Entity:DrawModel()
local dis = 512
hook.Add( "PreDrawHalos", "AddHalos", function()
	--cache a trace
	local tr = LocalPlayer():GetEyeTrace()

	--make sure the trace hit an entity, make sure that  it didn't hit the world, then we get teh squared distance which is way faster than getting the regular distance :D
	if (tr.Entity and tr.HitNonWorld and IsValid(tr.Entity) and LocalPlayer():GetPos():DistToSqr(tr.Entity:GetPos()) <= (dis * dis)) then
        halo.Add({self.Entity}, Color( 255, 0, 0 ))
    end
end)
end


local function DrawTheMenu()
if ( !frame ) then
		local frame = vgui.Create( "DFrame" )
		frame:SetSize( 280, 280 )
		frame:Center()
		frame:SetVisible( true )
		frame:MakePopup()
		frame:SetTitle( "Drug Lab" )
		
		frame:SetDeleteOnClose( true )
		
		
		buttonLSD = vgui.Create( "DButton", frame )
		buttonLSD:SetText( "LSD" )
		buttonLSD:SetSize( 110, 50 )
		buttonLSD:SetPos( 20, 45 )
		buttonLSD.DoClick = function()
		net.Start( "druglab_toserver" )
			net.WriteString( "durgz_lsd" )
		net.SendToServer()
		end
			
			
		buttonHeroin = vgui.Create( "DButton", frame )
		buttonHeroin:SetText( "Heroin" )
		buttonHeroin:SetSize( 110, 50 )
		buttonHeroin:SetPos( 20, 100 )
		buttonHeroin.DoClick = function()
		net.Start( "druglab_toserver" )
			net.WriteString("durgz_heroin")
		net.SendToServer()
		end
		
		buttonMeth = vgui.Create( "DButton", frame )
		buttonMeth:SetText( "Meth" )
		buttonMeth:SetSize( 110, 50 )
		buttonMeth:SetPos( 20, 155 )
		buttonMeth.DoClick = function()
		net.Start( "druglab_toserver" )
			net.WriteString("durgz_meth")
		net.SendToServer()
		end
		
		buttonCocaine = vgui.Create( "DButton", frame )
		buttonCocaine:SetText( "Cocaine" )
		buttonCocaine:SetSize( 110, 50 )
		buttonCocaine:SetPos( 20, 210 )
		buttonCocaine.DoClick = function()
		net.Start( "druglab_toserver" )
			net.WriteString("durgz_cocaine")
		net.SendToServer()
		end

		buttonPCP = vgui.Create( "DButton", frame )
		buttonPCP:SetText( "PCP" )
		buttonPCP:SetSize( 110, 50 )
		buttonPCP:SetPos( 150, 45 )
		buttonPCP.DoClick = function()
		net.Start( "druglab_toserver" )
			net.WriteString("durgz_pcp")
		net.SendToServer()
		end
		
		buttonMushroom = vgui.Create( "DButton", frame )
		buttonMushroom:SetText( "Mushroom" )
		buttonMushroom:SetSize( 110, 50 )
		buttonMushroom:SetPos( 150, 100 )
		buttonMushroom.DoClick = function()
		net.Start( "druglab_toserver" )
			net.WriteString("durgz_mushroom")
		net.SendToServer()
		end
		
		buttonWeed = vgui.Create( "DButton", frame )
		buttonWeed:SetText( "Weed" )
		buttonWeed:SetSize( 110, 50 )
		buttonWeed:SetPos( 150, 155 )
		buttonWeed.DoClick = function()
		net.Start( "druglab_toserver" )
			net.WriteString("durgz_weed")
		net.SendToServer()
		end
		
		buttonCigarettes = vgui.Create( "DButton", frame )
		buttonCigarettes:SetText( "Cigarettes" )
		buttonCigarettes:SetSize( 110, 50 )
		buttonCigarettes:SetPos( 150, 210 )
		buttonCigarettes.DoClick = function()
		net.Start( "druglab_toserver" )
			net.WriteString("durgz_cigarette")
		net.SendToServer()
		end
		
		
	end
end

usermessage.Hook( "DrawTheMenu", DrawTheMenu )


SHARED.LUA





ENT.Base = "lab_base"
ENT.PrintName = "Drug Lab"

function ENT:initVars()
    self.model = "models/props_lab/crematorcase.mdl"
    self.initialPrice = 100
    self.labPhrase = DarkRP.getPhrase("drug_lab")
    self.itemPhrase = DarkRP.getPhrase("drugs")
    self.noIncome = true
    self.camMul = -39
end


Instead of having




net.Receive( "druglab_toserver", ENT:createItem )

function ENT:createItem()
    local drugType = net.ReadString()
    local drugPos = self:GetPos()
    local drug = ents.Create(drugType)
    drug:SetPos(Vector(drugPos.x, drugPos.y, drugPos.z + 35))
    drug.nodupe = true
    drug:Spawn()
end


Use




net.Receive( "druglab_toserver", ENT:createItem, function ENT:createItem()

    local drugType = net.ReadString()
    local drugPos = self:GetPos()
    local drug = ents.Create(drugType)
    drug:SetPos(Vector(drugPos.x, drugPos.y, drugPos.z + 35))
    drug.nodupe = true
    drug:Spawn()
end)


I’m new to coding, but give this a try.

You can’t declare functions that way. Just declare the function outright as a callback and don’t put it in the ENT table. You’ll have to network the entity you want to use, however.

I think this might work actually:



net.Receive( "druglab_toserver", ENT.createItem )

function ENT:createItem()
    local drugType = net.ReadString()
    local drugPos = self:GetPos()
    local drug = ents.Create(drugType)
    drug:SetPos(Vector(drugPos.x, drugPos.y, drugPos.z + 35))
    drug.nodupe = true
    drug:Spawn()
end


If not, try this:
:snip: nevermind

[editline]29th October 2017[/editline]

ninja’d

self isn’t defined.

still doesn’t work…

bump!

Post your updated code.