Encountering problems server lag with my code.

I’m working on this but would like input

I have tried porting the code to client and using Datastream to send the changed velocity to set. But Datastream is to slow.

It needs to:
(GetVel, Process, SendDataSteam, SetVel)
(GetVel, Process, SendDataSteam, SetVel) …

With Data steam its process like:
GetVel, Process, SendDataSteam, GetVel, Process, SendDataSteam, SetVel, SetVel

This leads me to believe I need to go back to all server processed code and having the client do some of the work is bad


function GM:OnPlayerHitGround( ply )
	if !ply:Alive() then return end
	if (CurTime() < ply.DuckSlideTime) then return end
	if !ply:KeyDown(IN_DUCK) then return end
	if !ply:KeyDown(IN_FORWARD) then return end
	
	print("Slide")
	ply.IN_SLIDE = true
	ply.PlayerVel = ply:GetVelocity()
	local Decel = 10
	ply:SetCrouchedWalkSpeed(0)
	ply.DuckSlideTime = CurTime() + 2
	ply.OldPos_Z = ply:GetPos().z
	
	//Debug
	ply.SlideLength = 0
	
	timer.Create( "DuckSlideTimer", .025, 0, function()
		if !ply:KeyDown(IN_DUCK) then 
			ply:SetCrouchedWalkSpeed(1) 
			ply.IN_SLIDE = false
			print("SlideLenght")
			print(ply.SlideLength)
			timer.Destroy("DuckSlideTimer") 
			return 
		end
		
		if !ply:KeyDown(IN_FORWARD) then 
			ply:SetCrouchedWalkSpeed(1) 
			ply.IN_SLIDE = false
			print("SlideLenght")
			print(ply.SlideLength)
			timer.Destroy("DuckSlideTimer")
			return 
		end
		
		if !ply:OnGround() then
			return 
		end
		
		if ply.PlayerVel:Length() == 0 then  return end
		
		//Nominal Decel
		ply.PlayerVelNorm = ply.PlayerVel:GetNormalized()
		ply.PlayerDeltaVel = ply.PlayerVelNorm * Decel
		ply.PlayerVel = ply.PlayerVel - ply.PlayerDeltaVel
		
		
		// Speed / Height Proccesing
		ply.CurPos_Z = ply:GetPos().z
		ply.DeltaPos_Z = math.abs(ply.CurPos_Z - ply.OldPos_Z)
		ply.DeltaVel = (ply.PlayerVelNorm * ply.DeltaPos_Z * .25)
		
		print(ply:GetVelocity():Length())
		
		if ply.CurPos_Z < ply.OldPos_Z then // We are going down hill
			ply.PlayerVel = ply.PlayerVel + ( ply.DeltaVel * 6)
		elseif ply.CurPos_Z > ply.OldPos_Z then // We are going up hill
			ply.PlayerVel = ply.PlayerVel - ply.DeltaVel
		end
		
		// Set up for next cycle
		ply.OldPos_Z = ply.CurPos_Z
		
		
		//Clamp Vel To zero if velocity is close to zero
		if ply.PlayerVel:Length() <= Decel then ply.PlayerVel = Vector(0,0,0) end
		
		//Clamp Vel To zero if velocity is close to zero because he hit something in the world
		if ply:GetVelocity():Length() <= 75 then ply.PlayerVel = Vector(0,0,0) end
		
		ply.SlideLength = math.Round(ply.SlideLength + ply.PlayerVel:Length())
		// Set the our veloicty in the engine
		ply:SetLocalVelocity(ply.PlayerVel)	
	end )
end

http://www.garry.tv/?p=1198