Client -> Server

So, I’ve hit a big roadblock in my gamemode, and I can’t seem to figure out a good way to have a client send data to the server.

What I’m working with:

  • clientside function gui.ScreenToVector
  • serverside function which has to use the returned vector

How can I use that vector on the server?

--------CLIENT--------


function GM:GUIMousePressed(mouse)
	if mouse == 107 then
		LocalPlayer():SetNWVector("AimVector", gui.ScreenToVector(gui.MousePos())) -- This vector I need to use on the server.
		LocalPlayer():ConCommand("UnitSpawn")
	end
end


--------SERVER--------


function UnitSpawn(ply)
	local pos = ply:GetShootPos()
	local ang = ply:GetNWVector("AimVector") -- On the server, it is always seen just as Vector(0.0000, 0.0000, 0.0000)
	local tracedata = {}
	tracedata.start = pos
	tracedata.endpos = pos+(ang*1000)
	for k,v in pairs(ents.FindByClass("func_wall")) do
		tracedata.filter = {ply, v}
	end
	local trace = util.TraceLine(tracedata)
	if trace.HitWorld then
		local barrel=ents.Create("unit")
		barrel:SetModel("models/props_c17/oildrum001.mdl")
		barrel:SetPos(Vector(trace.HitPos.x,trace.HitPos.y,trace.HitPos.z+1))
		barrel:Spawn()
		barrel:SetNWEntity("owner", ply)
		barrel:SetNWString("owner_nick", ply:Nick())
	end
end
concommand.Add("UnitSpawn", UnitSpawn)

As you can see, I tried using NWVars with no luck. The Vector set on the client is seen as Vector(0.0000, 0.0000, 0.0000) on the server.

Any help at all would be greatly appreciated, thanks!

Client to server networking is done througj console commands, most of the time. That being said view angles are networked so I’m sure you could just retrieve them serverside using player.EyeAngles or something similar.

Still I tweaked your code to show you how to send and receive data in console commands :

--------CLIENT--------


function GM:GUIMousePressed(mouse)
	if mouse == 107 then
		local v = gui.ScreenToVector(gui.MousePos())
		LocalPlayer():ConCommand("UnitSpawn "..v.x.." "..v.y.." "..v.y)
	end
end


--------SERVER--------


function UnitSpawn(ply,cmd,args)
	local pos = ply:GetShootPos()
	local ang = Vector(args[1], args[2], args[3])
	local tracedata = {}
	tracedata.start = pos
	tracedata.endpos = pos+(ang*1000)
	for k,v in pairs(ents.FindByClass("func_wall")) do
		tracedata.filter = {ply, v}
	end
	local trace = util.TraceLine(tracedata)
	if trace.HitWorld then
		local barrel=ents.Create("unit")
		barrel:SetModel("models/props_c17/oildrum001.mdl")
		barrel:SetPos(Vector(trace.HitPos.x,trace.HitPos.y,trace.HitPos.z+1))
		barrel:Spawn()
		barrel:SetNWEntity("owner", ply)
		barrel:SetNWString("owner_nick", ply:Nick())
	end
end
concommand.Add("UnitSpawn", UnitSpawn)

Awesome, thanks that is very helpful

LocalPlayer():ConCommand() is depricated, use RunConsoleCommand() :stuck_out_tongue: