Bad argument #1 to '__add' (Vector expected, got number)

I’m trying to make it so that in my gamemode the “Zones” will cap the speed at 170 but, I get that error.

What i’m trying to do:


	
	local vel = ply:GetVelocity():Length2D()
	vel = vel + (wishdir * accelspeed)
	if (ply.InSpawn && vel:Length2D() > 100) then
		data:SetVelocity(ply:GetVelocity() * (100/vel))
	end
	
	data:SetVelocity( vel )
	return false

What the old code was:


	local vel = data:GetVelocity()
	vel = vel + (wishdir * accelspeed)
	if(ply.InSpawn && vel:Length2D() > 170) then
		vel = Vector(0,0,0)
	end
	data:SetVelocity(vel)

	return false

But the this code makes it so it stops you if you get 170 i’m trying to make it so that it doesn’t stop you and keeps it at 180 max. Like a cap.

Well, you see… when you add a number with a vector it won’t work! There’s simply no way that would ever work!

What are you trying to do?

I’m trying to make it so that it doesn’t just stop you to Vector(0,0,0) does. I’m trying to make it just cap at it.

Like this: but im trying to get it it work with my code…


local MAX_SPEED = 170
function speedLimiter(ply, data)
	local speed = ply:GetVelocity():Length()
	if speed > MAX_SPEED then
		data:SetVelocity(ply:GetVelocity() * (MAX_SPEED/speed))
	end
end
hook.Add("Move", "Speed Limit", speedLimiter)

Use velocity:GetNormal() * MAX_SPEED instead (basically what you’re doing anyways).


local MAX_SPEED = 170
function speedLimiter(ply, data)
	local speed = ply:GetVelocity():Length()
local getvel = ply:GetVelocity()
	if speed > MAX_SPEED then
		ply:SetVelocity( Vector( math.Clamp( getvel.x, -MAX_SPEED, MAX_SPEED), math.Clamp( getvel.y, -MAX_SPEED, MAX_SPEED), math.Clamp( getvel.z, -MAX_SPEED, MAX_SPEED) ) )
	end
end
hook.Add("Move", "Speed Limit", speedLimiter)

is a viable if somewhat ugly to look at alternative

No this is mathematically wrong.


local MAX_SPEED = 170
function speedLimiter(ply, data)
	local getvel = ply:GetVelocity()
	local speed = speed:Length()
	if speed > MAX_SPEED then
		local wishvel = speed / MAX_SPEED
		data:SetVelocity( getvel / wishvel )
	end
end
hook.Add("Move", "Speed Limit", speedLimiter)

Untested but should work. Beware of the consequence that it will reduce fall speeds to 170 as well.

Got it working thanks for the help