[Lua] Using the Move Hook to Change the Player's Speed

I’m trying to use the move hook to change the player’s max speed in my script, but for some reason it’s making the player go hyper speed. I’ve checked the math in a calculator and it doesn’t make any sense so maybe someone here can see the error.

[lua]
if SERVER then
hook.Add(“Move”, “VNTWeaponWeightSpeedMod”, function(_P)
local _PLYSPEEDWALK = _P:GetWalkSpeed()
local _PLYSPEEDRUN = _P:GetRunSpeed()
local _CURWEIGHT = _P:GetNWInt( “VNTWeaponWeightCurrent” )
local _PLYSPEEDWALKNEW = ( _PLYSPEEDWALK / _CURWEIGHT )
local _PLYSPEEDRUNNEW = ( _PLYSPEEDRUN / _CURWEIGHT )
if (((_P:KeyDown(IN_FORWARD) or _P:KeyDown(IN_BACK) or _P:KeyDown(IN_MOVELEFT) or _P:KeyDown(IN_MOVERIGHT))) and (_P:GetNWInt(“VNTWeaponWeightCurrent”) > GetConVarNumber(“VNT_WeaponWeight_Max”)) ) then
_P:SetWalkSpeed( _PLYSPEEDWALKNEW )
_P:SetRunSpeed( _PLYSPEEDRUNNEW )
end
end)
end
[/lua]

The function works, but the speed it’s setting the player to is exponentially higher than the default causing you to go airbourne. Can anyone explain what I did wrong?

In the calculator, the walk speed (250) divided by the weapon weight, say 45, comes out to 5, but for some reason no matter what speed I give it the player’s speed goes berserk.

Try printing the value of _CURWEIGHT right after setting the variable; print(_CURWEIGHT) and see what it says in the console. Maybe it comes out as 0.45, which when divided by, would increase the speed?

Like bitches said, if CURWEIGHT is less than 1, it will make the player faster. Also, I think that GetWalkSpeed returns the current speed, so if you increase it, the next time the hook is executed it will return a higher number, and the speed will be increased infinitely. Reducing player speed this way will make it unable to move.

Adding to the post above, either

CMoveData:GetMaxClientSpeed or

CMoveData:GetMaxSpeed are what you’re looking for, along with their respective setters, this way you’re temporarily modifying their speed.

Try this:


if SERVER then
	hook.Add("Move", "VNTWeaponWeightSpeedMod", function(_P,mv)
		local _PLYSPEED	= mv:GetMaxSpeed()
		local _CURWEIGHT = _P:GetNWInt( "VNTWeaponWeightCurrent" )
		local _PLYSPEEDNEW = ( _PLYSPEED / _CURWEIGHT )
		if (((_P:KeyDown(IN_FORWARD) or _P:KeyDown(IN_BACK) or _P:KeyDown(IN_MOVELEFT) or _P:KeyDown(IN_MOVERIGHT))) and (_P:GetNWInt("VNTWeaponWeightCurrent") > GetConVarNumber("VNT_WeaponWeight_Max")) ) then	
			mv:SetMaxSpeed( _PLYSPEEDNEW )
		        mv:SetMaxClientSpeed( _PLYSPEEDNEW )
		end
	end)
end

And make sure that CURWEIGHT is higher than 1. If not, you could try multiplying CURWEIGHT by 10 or 100.

I’ll try that first.

That’s the point. If the player has too much weight carried, it will slow them down based on how much they’re carrying, so if they drop some weight it’s supposed to allow them to move again.