Variable Zoom for Scripted Weapons

I created a thread for it because this is a pretty huge issue that I’ve been having with my scripted weapons. I’m having a massive problem with net probably because I don’t understand it. I’m basically trying to create variable zoom for my scripted weapon base but it brings in a huge amount of problems.

The current problem is that it’s choppy. I’m just wondering if there are any alternatives or fixes to my current code. I added in code comments to make it easier for anyone who wishes to help.

[lua]
function SWEP:ZoomOut(delay) – Called when someone exits viewing from the scope
if SERVER then
if delay == nil then
delay = 0
end

	self.Owner:DrawViewModel(true)
	self:SetNWBool("zoomed",false) -- tells clientside/serverside that this weapon is not zoomed
	self.Owner:SetFOV(self.Owner:GetNWInt("desiredfov"),delay) -- desiredfov is a variable where it gets the default FOV
end

end

function SWEP:ZoomIn(delay) – Called when someone enters viewing from the scope OR changes their zoom magnification
if SERVER then
if delay == nil then
delay = 0
end
self.Owner:DrawViewModel(false)
self:SetNWBool(“zoomed”,true) – tells clientside/serverside that this weapon is now zoomed
self.Owner:SetFOV(self.Owner:GetNWInt(“desiredfov”) / (self.ZoomAmount * self:GetNWFloat(“bonuszoom”,1) ),delay) – Changes Zoom.
–desiredfov = default fov,
–ZoomAmount is the static amount determined from each swep.
–bonuszoom is a dynamic zoom determined by mousewheel.
end
end

function SWEP:RegisterCommands() – called when someone deploys their weapon or when a weapon initializes

if SERVER then
	util.AddNetworkString( "GetFOVBonus" )
end

if game.SinglePlayer() then
	self.Owner:SetNWInt("desiredfov",GetConVarNumber("fov_desired"))
return end

if SERVER then
	util.AddNetworkString( "GetFOV" )

	function GetData(len,ply)
		local data = net.ReadInt(8) -- this is the default player zoom
		local ent = net.ReadEntity() -- this is the weapon entity that it came from
		
		if ent ~= self then return end
		
		--print is debug information
		print("---------------") 
		print("FUNCTION GetData()")
		print("NET: RECIEVED "..data .. " FROM " .. string.upper(ply:Nick()) .. " FOR " .. string.upper(self.Owner:Nick()) ) 
		print("FROM " .. tostring(ent) .. " TO " .. tostring(self))
		print("---------------")
		
		
		ply:SetNWInt("desiredfov",data)
	end
	
	net.Receive( "GetFOV", GetData )
	
end

if CLIENT then
	local data = LocalPlayer():GetFOV()

	net.Start( "GetFOV" )
		net.WriteInt( data, 8 )
		net.WriteEntity(self)
	net.SendToServer()
	
end

end

function SWEP:ZoomFunctionThink() – called in SWEP:Think()

function AddBonus(len,ply)
	local data = net.ReadFloat(8) -- data is the bonus zoom determined by the mousewheel scroll
	local ent = net.ReadEntity() --the weapon that called it
	
	if ent ~= self then return end
	
	print("---------------")
	print("FUNCTION AddBonus()")
	print("NET: RECIEVED ".. data .. " FROM " .. string.upper(ply:Nick()) .. " FOR " .. string.upper(self.Owner:Nick()) ) 
	print("FROM " .. tostring(ent) .. " TO " .. tostring(self))
	print("---------------")
	
	self:SetNWFloat("bonuszoom",data)
	
	self:ZoomIn()
end
	
net.Receive( "GetFOVBonus", AddBonus )

if CLIENT then
	if self:GetNWBool("zoomed",false) == true then
		
		if input.WasMousePressed(MOUSE_WHEEL_UP) then
			
			if self.ZoomData < 1.5 then 
				self.ZoomData = self.ZoomData + 0.25/2
				self:EmitSound("common/talk.wav")
			end
			
			net.Start( "GetFOVBonus" )
				net.WriteFloat(self.ZoomData, 8 )
				net.WriteEntity(self)
			net.SendToServer()

		elseif input.WasMousePressed(MOUSE_WHEEL_DOWN) then
			
			if self.ZoomData > 0.5 then 
				self.ZoomData = self.ZoomData - 0.25/2
				self:EmitSound("common/talk.wav")
			end
			
			net.Start( "GetFOVBonus" )
				net.WriteFloat( self.ZoomData, 8 )
				net.WriteEntity(self)
			net.SendToServer()
			
		end
	end
end

end
[/lua]

Is there any possible way of reducing the lag/choppiness for any of this?

that looks over-complicated as hell lol

i would say to use datatables and TranslateFOV

I think I’ll draft up something up

Omg that’s much simpler.
When I saw that grabbing the fov was a server side function I was like wtf. I’m glad there is a clientside function.

I’ll leave this thread open for now just in case I encounter any other issues.

Ok I manged to get everything working, but im not even going to bother with my way of variable zoom right now. Thanks for the help.