
TOOL.ClientConVar[ "angularlimit" ] = 100000

function TOOL:LeftClick( trace )

	if (!trace.Entity:IsValid() || trace.Entity:IsPlayer() ) then return false end

	// If there's no physics object then we can't constraint it!
	if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
	
	if ( CLIENT ) then return true end

	local Phys = trace.Entity:GetPhysicsObjectNum( trace.PhysicsBone )
	
	// Get client's CVars
	local angularlimit	= tonumber( self:GetClientInfo( "angularlimit" ) )
	
	// Get information we're about to use
	local Ent  = trace.Entity
	local Bone = trace.PhysicsBone
	local Ang  = Phys:GetAngle()

	local constr = constraint.Keepupright( Ent, Ang, Bone, angularlimit, systemmanager )
	
		if (constr) then
		undo.Create("KeepUpright")
		undo.AddEntity( constr )
		undo.SetPlayer( self:GetOwner() )
		undo.Finish()
	
		self:GetOwner():AddCleanup( "constraints", constr )
	
	end
	
	return true
	
end

function TOOL:RightClick( trace )

	if (!trace.Entity:IsValid() || trace.Entity:IsPlayer() ) then return false end
	if ( CLIENT ) then return true end
	
	constraint.RemoveConstraints( trace.Entity, "Keepupright" )
	return true

end
