Laggy setting players velocity

I have been working on this code for a bit and would like some help. I know i’m doing something wrong but i don’t know what.

I’m mostly sure that the lag has nothing to do with the timer getting called every .025 (and i do not want to do the math to often) of a sec because the code runs quite smoothly on the server-client. but people that are not on the same comp as the server get jiggys and lag


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 PlayerVel 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.PlayerVel.z = 0
		ply:SetLocalVelocity(ply.PlayerVel)	
	end )
end

The code looks rather wierd… <.>, and yes, changing movement speeds or something via Multiplyers like i have it just makes the movement lag out of Single Player, Looks smooth when other players are moving, but from there view it looks laggy, Thanks to garrys new tf2 movement code :3:

You have to make it shared.

This. And honestly, it’s not a big issue, it’s just something people neglect all too often.

You think i didnt try that? all my move hooks are in shared, And hosting a mutliplayer, Or using it on a dedicated had no effect what so ever, Im not sure whats wrong though, I can post the code if you seriouslly want to figure out whats wrong with it.

Did you give the client all the variables it needs to compute it the right way? Sometimes they differ a bit!

You could also try interpolating the movements of the clients on their end.

Not sure, But ill be sure to give that a shot, Cheers for the info :3:

Look at GMove’s code. It could be useful as it’s meant to be fully predicted.

ply:SetRunSpeed( speed )
ply:SetWalkSpeed( speed )
ply:SetCrouchSpeed( speed )

Those set the speeds of running/walking/crouching. If that’s what you’re trying to do. They work smoothly clientside and serverside for me.

Also, most likely this information has nothing to do what you want because I have a terrible headache. But that’s what I’m thinking from just looking over the thread.

Also, in RickDark’s old LightRP script it had the same issue, but when I just replaced the velocity with these it worked smoothly.

Yes, but you can’t use those to propel the player.

Do it in the -snip- Move hook then!

I use the Move Hook

Yeah move.

And its still laggy :stuck_out_tongue:

is cl_predict 1?

How will making it shared help at all. If you do the math work on the client then you have to send the data to the server which is imposable to do reliably and quickly

its that really a function because the only function i know of is
ply:SetCrouchedWalkSpeed(multiplier)

and this ^ as far as I have tried I cant make you move faster then 200 units/s

Making it shared means that the client predicts what the server tells it, allowing for a lag-free transition of speeds. If you make it serverside only, it will send lots of speed data to the client without the client knowing otherwise. If you make it clientside only, it will not work because the client relies on the server for important things like speed and velocity to decide collisions and such.

I completely agree and understand up to the point where the server has to set the client’s velocity. I have tried all the methods of sending the data but they are all laggy and unreliable.

If the file is shared how do i tell the server the velocity to set

In a shared file do simply the same Maths, and for exemple the SetVelocity do it on the client too! The fact that source is so powerfull in multiplayer is ebcause you calculate half of the things too!(Sometimes it doesnt work but that is something else.)